如何读取BindingSource的托管对象类型 [英] How to read object type hosted by bindingSource
问题描述
编辑:我改变措辞,增加长采样code是更具描述性
我需要读通过的BindingSource对象绑定的类型名称。
我的方法的接受的BindingSource作为参数和它不知道的对象类型托管通过BindingSource的。但我需要阅读的对象类型
要更好地解释我的意思,以为我有2个班
类Person {
公共字符串名称{;组; }
公开名单<家长和GT;家长{获得;组; }
}
一流的家长{
公共字符串名称{;组; }
公众诠释ChildrenCount {获得;组; }
}
比我使用他们的Windows窗体绑定方案:
//创建者名簿
名单<人>人员=新的名单,其中,人物>();
//添加的样本数据
Persons.Add(新的Person(){名称=Person_1});
Persons.Add(新的Person(){名称=Person_2});
人[0]。家长=新的名单,其中,家长和GT;();
人[0] .Parents.Add(新的父(){名称=Father_1,ChildrenCount = 2});
人[0] .Parents.Add(新的父(){名称=Mother_1,ChildrenCount = 2});
人[1]。家长=新的名单,其中,家长和GT;();
人[1] .Parents.Add(新的父(){名称=Father_2,ChildrenCount = 1});
人[1] .Parents.Add(新的父(){名称=Mother_2,ChildrenCount = 1});
//创建绑定源
BindingSource的BS1 =新的BindingSource(人,NULL);
BindingSource的BS2 =新的BindingSource(BS1,家长);
//绑定到网格
dataGridView1.DataSource = BS1;
dataGridView2.DataSource = BS2;
// *******
// ******读类型托管由BS ********
// *******
// BS1 - 预期:System.Collections.Generic.List`1 [人]
// 那很简单...
Console.WriteLine(式结合到BS1 =+ bs1.DataSource.GetType());
// BS2 - 预期:System.Collections.Generic.List`1 [人]
//如何读取?? !!
//这个返回的BindingSource类型
Console.WriteLine(式结合到BS2 =+ bs2.DataSource.GetType());
//这个返回:System.Collections.Generic.List`1 [人](我需要的名单,其中,家长>或Person.List<家长及GT;
Console.WriteLine(式结合到BS2 =+(bs2.DataSource如的BindingSource).DataSource.GetType());
因此,当你发现这是主从绑定(BS1被绑定到一个网格,BS2第二)*
所以我想为不知怎么读键入通过BindingSource的BS2'托管'(预期类型列表<父母>) P>
方法我需要编写应看起来如下:
键入GetBSType(BindingSource的BS)
鸭preciate任何帮助......
您也可以寻找一种方式,以precisely标识类型。要确定一个的BindingSource BS
有一个数据源
这是一个名单,其中,家长和GT;
,你会做以下内容:
如果(bs.DataSource.GetType()== typeof运算(名单<家长和GT;))
{
//做些有益的事
}
此外,发现如果事情是从一个派生类(或某事实现特定的接口),使用:
如果(typeof运算(MyFancyType).IsAssignableFrom(bs.DataSource.GetType()))
{
//做些有益的事
}
IsAssignableFrom
返回true,如果与通过类型的变量可以存储在类型的变量 MyFancyType
。例如:
如果(typeof运算(对象).IsAssignableFrom(typeof运算(字符串)))
{
//做些有益的事
}
总是返回true,做一些有用的东西。 (因为你能坚持一个字符串
对象引用到对象
变量)
其中:
如果(typeof运算(字符串).IsAssignableFrom(typeof运算(对象)))
{
//做些有益的事
}
绝不会做任何事情有用。 (你不能坚持一个通用的对象
对象引用到字符串
变量...(在编译时或不铸造))
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...
You may also be looking for a way to precisely identify the type. To determine that a BindingSource bs
has a DataSource
that is a List<Parent>
, you would do the following:
if (bs.DataSource.GetType() == typeof(List<Parent>))
{
//Do something useful
}
Furthermore, to find if something is derived from a type (or that something implements a specific interface), use:
if (typeof(MyFancyType).IsAssignableFrom(bs.DataSource.GetType()))
{
//Do something useful
}
IsAssignableFrom
returns true if a variable with the passed type can be stored in a variable of type MyFancyType
. For example:
if (typeof(object).IsAssignableFrom(typeof(string)))
{
//Do something useful
}
Would always return true and do "something useful". (Because you can stick a string
object reference into an object
variable)
Where:
if (typeof(string).IsAssignableFrom(typeof(object)))
{
//Do something useful
}
Would never do anything "useful". (you can't stick a generic object
object reference into a string
variable...(at compile time or without a cast))
这篇关于如何读取BindingSource的托管对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!