Trying to get List<T> through reflection
-
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; }
}
-
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; }
}
-
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; }
}
joost.versteegen wrote:
give me a hint
Avoid Reflection.
You'll never get very far if all you do is follow instructions.
-
Are there any problems remaining after the cast to
List<object>
is replaced withList<Person>
? Alan. EDIT hope that ok now, was having a problem with the angle brackets around Person but not object!!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").
-
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").
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.
-
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.
you're right! it works. thanks! i was thinking too complex i guess.
-
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; }
}
Even without using reflection, you won't be able to cast a
List<Person>
to aList<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 aPerson
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