2009-08-19 50 views
1

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) 

感謝所有幫助...

+0

你能更好地闡明你的意圖嗎?代碼似乎試圖實現一些沒有描述的東西,我們可能會爲您提供的示例提供一種替代方法。 – STW 2009-08-19 15:59:40

+0

@Yoooder - 同意...該示例是菊花鏈綁定源。從描述,我期望:'bs2.DataSource =新列表();' – el2iot2 2009-08-19 16:20:48

回答

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的數據源,而不是bs`本身:

// This is essentially binding bs2 to the underlying List<Person> 
bs2.DataSource = bs1.DataSource; 
+0

感謝您的評論 - 對不起,我不是在我的問題不精確。我編輯過它並添加了更好的示例。希望現在很明顯,問題是與bs.DataMember而不是DataSource - 因爲它總是指向列表我需要得到什麼BS2是'託管' - >在這種情況下Person.Parents - >類型我想閱讀將列表。任何想法如何獲得? – Maciej 2009-08-19 21:53:18

1

您也可能會尋找一種方法來準確識別的類型。要確定一個BindingSource bs具有DataSource這是一個List<Parent>,你會做以下幾點:

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

此外,發現如果事情是從一個類型派生的(或某事實現一個特定的接口),使用:

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

IsAssignableFrom返回true如果與傳遞的類型的變量可以存儲在MyFancyType類型的變量。例如:

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

總會返回true並做「有用的事情」。 (因爲你可以堅持一個string對象引用到object變量)

其中:

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

絕不會做任何事情 「有用」。 (你不能將通用的object對象引用粘貼到變量string中(在編譯時或沒有投射))