EDIT託管對象類型:我改變措詞,加入長的樣品代碼是更具描述性的如何讀取通過的BindingSource
我需要讀取通過的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());
因此,你注意到這是Master-Detail綁定(bs1綁定到一個網格,bs2到第二個)*
所以我想讀取某種類型'hosted'通過bindingSou RCE BS2(預期類型列表<家長>)
方法我需要寫款的外觀如下:
Type GetBSType (BindingSource bs)
感謝所有幫助...
你能更好地闡明你的意圖嗎?代碼似乎試圖實現一些沒有描述的東西,我們可能會爲您提供的示例提供一種替代方法。 – STW 2009-08-19 15:59:40
@Yoooder - 同意...該示例是菊花鏈綁定源。從描述,我期望:'bs2.DataSource =新列表();' –
el2iot2
2009-08-19 16:20:48