如何读取bindingSource 托管的对象类型

编辑:我更改了措辞,添加了长示例代码以更具描述性

我需要通过BindingSource读取对象绑定的类型名称。
我的方法接受BindingSource作为参数 ,它不知道BindingSource对象类型’托管’。 但我需要阅读该对象类型

为了更好地解释我的意思,假设我有2个class级

class Person { public string Name { get; set; } public List Parents { get; set; } } class Parent { public string Name { get; set; } public int ChildrenCount { get; set; } } 

比我在Windows窗体绑定方案中使用它们:

  // Create Person List List Persons = new List(); // add Sample data Persons.Add(new Person() { Name = "Person_1" }); Persons.Add(new Person() { Name = "Person_2" }); Persons[0].Parents = new List(); 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(); 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 or Person.List" Console.WriteLine("type bind to BS2=" + (bs2.DataSource as BindingSource).DataSource.GetType()); 

所以,你注意到这是Master-Detail绑定(bs1绑定到一个网格,bs2绑定到第二个)*

所以我想以某种方式读取类型’托管’通过bindingSource bs2 (预期类型是List )

我需要编写的方法如下:

 Type GetBSType (BindingSource bs) 

感谢任何帮助……

在.NET中,每个对象都inheritance自System.Object ,它有一个名为GetType()的方法,它将返回对象的类型 – 这可以解决你测试bs1的第一个问题

 Type bs1DataType = bs1.DataSource.GetType(); string bs1DataTypeName = bs1DataType.Name; 

第二个问题有点不清楚; 您可以使用相同的方法来确定bs2.DataSource的Type == BindingSource(因为您将其设置为bs1)。 然后,您可以将它从Object转换回BindingSource并查询它的DataSource属性以确定基础类型(基本上只是做与上面示例相同的事情来确定bs1的基础数据源类型)。

 // Since bs2's DataSource type is a BindingSource, you // can cast it as such and query it's underlying data-type as well BindingSource bs2DataBindingSource = (BindingSource)bs2.DataSource; Type bs2DataBindingSourceType = bs2DataBindingSource.DataSource.GetType(); Console.WriteLine(bs2DataBindingSourceType.Name); 

这一切都令人费解,因为你正在挖掘两个绑定源来找到底层类型。


你可以删除bs2,只使用从外部源接收的bs1吗? 如果您已经收到BindingSource那么将其包装在另一个BindingSource似乎很奇怪。

另一种选择是将bs2的DataSource设置为bs1的DataSource,而不是bs`本身:

 // This is essentially binding bs2 to the underlying List bs2.DataSource = bs1.DataSource; 

您可能还在寻找一种精确识别类型的方法。 要确定BindingSource bs是否具有ListDataSource ,您将执行以下操作:

 if (bs.DataSource.GetType() == typeof(List)) { //Do something useful } 

此外,要查找是否从某个类型(或某些实现特定接口)派生某些内容,请使用:

 if (typeof(MyFancyType).IsAssignableFrom(bs.DataSource.GetType())) { //Do something useful } 

如果具有传递类型的变量可以存储在MyFancyType类型的变量中,则IsAssignableFrom返回true。 例如:

 if (typeof(object).IsAssignableFrom(typeof(string))) { //Do something useful } 

总是会返回真实并做“有用的东西”。 (因为您可以将string对象引用粘贴到object变量中)

哪里:

 if (typeof(string).IsAssignableFrom(typeof(object))) { //Do something useful } 

永远不会做任何“有用”的事情。 (你不能将通用object引用引用到string变量中……(在编译时或没有强制转换))