EDIT: I changed wording, added long sample code to be more descriptive
I need to read type name of object bind via BindingSource.
My method accepts BindingSource as parameter and it doesnt know about object type 'hosted' by BindingSource. But I need to read that object type
To explain better what I meant, assume I have 2 classes
class Person {
public string Name { get; set; }
public List<Parent> Parents { get; set; }
}
class Parent {
public string Name { get; set; }
public int ChildrenCount { get; set; }
}
Than I'm using them in Windows Forms binding scenario:
// Create Person List
List<Person> Persons = new List<Person>();
// add Sample data
Persons.Add(new Person() { Name = "Person_1" });
Persons.Add(new Person() { Name = "Person_2" });
Persons[0].Parents = new List<Parent>();
Persons[0].Parents.Add(new Parent() { Name = "Father_1", ChildrenCount = 2 });
Persons[0].Parents.Add(new Parent() { Name = "Mother_1", ChildrenCount = 2 });
Persons[1].Parents = new List<Parent>();
Persons[1].Parents.Add(new Parent() { Name = "Father_2", ChildrenCount = 1 });
Persons[1].Parents.Add(new Parent() { Name = "Mother_2", ChildrenCount = 1 });
// create binding sources
BindingSource bs1 = new BindingSource(Persons, null);
BindingSource bs2 = new BindingSource(bs1, "Parents");
// bind to grid
dataGridView1.DataSource = bs1;
dataGridView2.DataSource = bs2;
// ****************************************
// ****** Read type 'hosted' by BS ********
// ****************************************
// BS1 - Expected: System.Collections.Generic.List`1[Person]
// That's easy...
Console.WriteLine("type bind to BS1=" + bs1.DataSource.GetType());
// BS2 - Expected: System.Collections.Generic.List`1[Person]
// HOW TO READ THAT ??!!
// this returns BindingSource type
Console.WriteLine("type bind to BS2=" + bs2.DataSource.GetType());
// this returns: System.Collections.Generic.List`1[Person] (I need List<Parents> or Person.List<Parents>"
Console.WriteLine("type bind to BS2=" + (bs2.DataSource as BindingSource).DataSource.GetType());
So, as you noticed this is Master-Detail binding (bs1 is bind to one grid, bs2 to second)*
So I would like to read somehow type 'hosted' via bindingSource bs2 (Expected type is List < Parent > )
Method I need to write shall looks as follow:
Type GetBSType (BindingSource bs)
Appreciate any help...