【问题标题】:How to read object type hosted by bindingSource [duplicate]如何读取由 bindingSource 托管的对象类型 [重复]
【发布时间】:2009-08-19 13:13:17
【问题描述】:

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

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

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

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; }
}

比我在 Windows 窗体绑定场景中使用它们:

        // 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()); 

所以,正如您所注意到的,这是主从绑定(bs1 绑定到一个网格,bs2 绑定到第二个网格)*

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

我需要写的方法如下:

Type GetBSType (BindingSource bs)

感谢任何帮助...

【问题讨论】:

  • 您能否更好地阐明您的意图?该代码似乎试图实现未描述的内容,我们也许可以为您提供的示例提供另一种方法。
  • @Yoooder - 同意...示例是菊花链绑定源。从描述中,我希望:bs2.DataSource = new List&lt;Parent&gt;();

标签: c# .net reflection binding


【解决方案1】:

在 .NET 中,每个对象都继承自 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<Person>
bs2.DataSource = bs1.DataSource; 

【讨论】:

  • 感谢您的评论 - 抱歉,我的问题并不准确。我已经对其进行了编辑并添加了更好的示例。希望现在很明显问题出在 bs.DataMember 而不是 DataSource - 因为它总是指向 List 我需要获取 BS2 是“托管”的信息 -> 在这种情况下 Person.Parents -> type I'想阅读的是 List。知道如何获得吗??
【解决方案2】:

您可能还在寻找一种精确识别类型的方法。要确定BindingSource bs 中的DataSourceList&lt;Parent&gt;,您可以执行以下操作:

if (bs.DataSource.GetType() == typeof(List<Parent>))
{
  //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
}

总是会返回 true 并做一些“有用的事情”。 (因为您可以将 string 对象引用粘贴到 object 变量中)

地点:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 2010-09-06
    相关资源
    最近更新 更多