Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Trying to get List<T> through reflection

Trying to get List<T> through reflection

Scheduled Pinned Locked Moved C#
question
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    joost versteegen
    wrote on last edited by
    #1

    hello, i'm trying to get a list through reflection and pass it to a procedure, but can't get it to work. Can anyone give me a hint please? here's what i ave sofar:

    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void Form1\_Load(object sender, EventArgs e)
    {
      Data data = new Data();
    
      string name = "Persons";
      Type type = data.GetType();
      PropertyInfo listProperty = type.GetProperty(name);
    
      //BindData<object>((List<object>)listProperty.GetValue(data, null));
    
      List<object> listObject = (List<object>)listProperty.GetValue(data, null);
    }
    
    private void BindData(List data)
    {
      comboBox1.DataSource = null;
      comboBox1.DataSource = data;
    }
    

    }

    public class Person
    {
    public string Name { get; set; }
    public int Age { get; set; }

    public Person(string name, int age)
    {
      Name = name;
      Age = age;
    }
    
    public override string ToString()
    {
      return Name;
    }
    

    }

    public class Data
    {
    public Data()
    {
    Persons = new List();
    Persons.Add(new Person("One", 1));
    Persons.Add(new Person("Two", 2));
    }

    public List Persons { get; set; }
    

    }

    A P Richard DeemingR 3 Replies Last reply
    0
    • J joost versteegen

      hello, i'm trying to get a list through reflection and pass it to a procedure, but can't get it to work. Can anyone give me a hint please? here's what i ave sofar:

      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeComponent();
      }

      private void Form1\_Load(object sender, EventArgs e)
      {
        Data data = new Data();
      
        string name = "Persons";
        Type type = data.GetType();
        PropertyInfo listProperty = type.GetProperty(name);
      
        //BindData<object>((List<object>)listProperty.GetValue(data, null));
      
        List<object> listObject = (List<object>)listProperty.GetValue(data, null);
      }
      
      private void BindData(List data)
      {
        comboBox1.DataSource = null;
        comboBox1.DataSource = data;
      }
      

      }

      public class Person
      {
      public string Name { get; set; }
      public int Age { get; set; }

      public Person(string name, int age)
      {
        Name = name;
        Age = age;
      }
      
      public override string ToString()
      {
        return Name;
      }
      

      }

      public class Data
      {
      public Data()
      {
      Persons = new List();
      Persons.Add(new Person("One", 1));
      Persons.Add(new Person("Two", 2));
      }

      public List Persons { get; set; }
      

      }

      A Offline
      A Offline
      Alan N
      wrote on last edited by
      #2

      Are there any problems remaining after the cast to List<object> is replaced with List<Person> ? Alan. EDIT hope that ok now, was having a problem with the angle brackets around Person but not object!!

      J 1 Reply Last reply
      0
      • J joost versteegen

        hello, i'm trying to get a list through reflection and pass it to a procedure, but can't get it to work. Can anyone give me a hint please? here's what i ave sofar:

        public partial class Form1 : Form
        {
        public Form1()
        {
        InitializeComponent();
        }

        private void Form1\_Load(object sender, EventArgs e)
        {
          Data data = new Data();
        
          string name = "Persons";
          Type type = data.GetType();
          PropertyInfo listProperty = type.GetProperty(name);
        
          //BindData<object>((List<object>)listProperty.GetValue(data, null));
        
          List<object> listObject = (List<object>)listProperty.GetValue(data, null);
        }
        
        private void BindData(List data)
        {
          comboBox1.DataSource = null;
          comboBox1.DataSource = data;
        }
        

        }

        public class Person
        {
        public string Name { get; set; }
        public int Age { get; set; }

        public Person(string name, int age)
        {
          Name = name;
          Age = age;
        }
        
        public override string ToString()
        {
          return Name;
        }
        

        }

        public class Data
        {
        public Data()
        {
        Persons = new List();
        Persons.Add(new Person("One", 1));
        Persons.Add(new Person("Two", 2));
        }

        public List Persons { get; set; }
        

        }

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        joost.versteegen wrote:

        give me a hint

        Avoid Reflection.

        You'll never get very far if all you do is follow instructions.

        1 Reply Last reply
        0
        • A Alan N

          Are there any problems remaining after the cast to List<object> is replaced with List<Person> ? Alan. EDIT hope that ok now, was having a problem with the angle brackets around Person but not object!!

          J Offline
          J Offline
          joost versteegen
          wrote on last edited by
          #4

          List<Person> works fine, List<object> does not. I get: Unable to cast object of type 'System.Collections.Generic.List`1[WindowsFormsApplication1.Person]' to type 'System.Collections.Generic.List`1[System.Object]' in both last lines on the main proc. i want it to work for other classes then Person also.So, BindData(list) or BindData(list), based on the name of the list property in class Data ("Persons" or "Cars").

          A 1 Reply Last reply
          0
          • J joost versteegen

            List<Person> works fine, List<object> does not. I get: Unable to cast object of type 'System.Collections.Generic.List`1[WindowsFormsApplication1.Person]' to type 'System.Collections.Generic.List`1[System.Object]' in both last lines on the main proc. i want it to work for other classes then Person also.So, BindData(list) or BindData(list), based on the name of the list property in class Data ("Persons" or "Cars").

            A Offline
            A Offline
            Alan N
            wrote on last edited by
            #5

            The Combobox.DataSource property is typed as Object and so you can infer that the Combobox must be checking the compatibility of the assigned data source. The only requirement is that the data source implements the non generic IList interface. You could cast to that but the combox box will accept the data source directly without casting. Alan.

            J 1 Reply Last reply
            0
            • A Alan N

              The Combobox.DataSource property is typed as Object and so you can infer that the Combobox must be checking the compatibility of the assigned data source. The only requirement is that the data source implements the non generic IList interface. You could cast to that but the combox box will accept the data source directly without casting. Alan.

              J Offline
              J Offline
              joost versteegen
              wrote on last edited by
              #6

              you're right! it works. thanks! i was thinking too complex i guess.

              1 Reply Last reply
              0
              • J joost versteegen

                hello, i'm trying to get a list through reflection and pass it to a procedure, but can't get it to work. Can anyone give me a hint please? here's what i ave sofar:

                public partial class Form1 : Form
                {
                public Form1()
                {
                InitializeComponent();
                }

                private void Form1\_Load(object sender, EventArgs e)
                {
                  Data data = new Data();
                
                  string name = "Persons";
                  Type type = data.GetType();
                  PropertyInfo listProperty = type.GetProperty(name);
                
                  //BindData<object>((List<object>)listProperty.GetValue(data, null));
                
                  List<object> listObject = (List<object>)listProperty.GetValue(data, null);
                }
                
                private void BindData(List data)
                {
                  comboBox1.DataSource = null;
                  comboBox1.DataSource = data;
                }
                

                }

                public class Person
                {
                public string Name { get; set; }
                public int Age { get; set; }

                public Person(string name, int age)
                {
                  Name = name;
                  Age = age;
                }
                
                public override string ToString()
                {
                  return Name;
                }
                

                }

                public class Data
                {
                public Data()
                {
                Persons = new List();
                Persons.Add(new Person("One", 1));
                Persons.Add(new Person("Two", 2));
                }

                public List Persons { get; set; }
                

                }

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #7

                Even without using reflection, you won't be able to cast a List<Person> to a List<object>, because that would let you break the list. Consider what would happen if this cast was allowed:

                List<Person> onlyContainsPeople = new List<Person>();
                List<object> containsAnyObject = (List<object>)onlyContainsPeople;
                containsAnyObject.Add(new Hippopotamus()); // <-- Not a Person!
                Person thePerson = onlyContainsnPeople[0];

                You now have a list which is guaranteed to only contain Person objects, but the only object in the list is not a Person object. Either the line which adds the item to the list would have to throw an exception, or the line which retrieves the item from the list would have to throw an exception. Neither exception would be obvious or expected. There are a few generic interfaces and delegates which, in .NET 4.0 or higher, are declared as covariant or contravariant, which will allow something similar to what you're trying to do:

                List<Person> onlyContainsPeople = new List<Person>();
                IEnumerable<object> containsAnyObject = onlyContainsPeople;

                Generic covariance only works for read-only interfaces/delegates - the generic type parameter is only ever returned, never passed in. Contravariance only works when the type parameter is only ever passed in, never returned. See Covariance and Contravariance in Generics[^] on MSDN for more information.


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups