2015-10-13 53 views
0

我已經創建瞭如下新的自定義的PropertyDescriptor,如何將DataColumnPropertyDescriptor轉換爲定製屬性描述符?

public class CustomPropertyDescriptor : PropertyDescriptor 
    {  


     public CustomPropertyDescriptor (string name, Attribute[] attribute) 
      : base(name, attribute) 
     { 

     } 

     ............ 

     ............ 
} 

,但是當我嘗試使用施放此DataColumnPropertydescriptor,它不能被強制轉換爲自定義屬性描述符。但它成功地鑄造到PropertyDescriptor(不定製屬性描述符)

有沒有可用的解決方案?

+0

真的是你想達到什麼目的? –

+0

嗨伊凡,我可以得到DataColumnPropertyDescriptor實例,我需要將這個對象與我的自定義屬性描述符一起進行一些自定義。我希望你能理解 –

回答

1

DataColumnPropertydescriptor不是繼承自您的CustomPropertyDescriptor。這是一個從PropertyDescriptor繼承,不知道你的自定義類東西:

internal sealed class DataColumnPropertyDescriptor : PropertyDescriptor 

想想這個繼承好像都DogCatAnimal繼承:

public class Dog : Animal 

public class Cat: Animal 

但狗是不是貓,和你不能投DogCat

Cat felix = new Cat(); 
Dog pluto = (Dog)felix; // Of course not working 
Animal someAnimal = (Animal)felix; // Cat is animal, so it works 

這就是你現在要做的。您只能創建的CustomPropertyDescriptor新實例,並手動複製從DataColumnPropertydescriptor例如一些數據,你必須:

DataColumnPropertydescriptor columnDescriptor = ... 
CustomPropertyDescriptor customDescriptor = 
    new CustomPropertyDescriptor(columnDescriptor.Name, columnDescriptor.Attributes); 
+0

謝謝..我可以理解,但我無法找到方式來施放DataColumnPropertyDescriptor。因爲DataColumnPropertyDescriptor是封閉類,我不能得到它的成員等...可以請你提出任何解決方案? –

+0

將'DataColumnPropertyDescriptor'的實例轉換爲'PropertyDescriptor'並使用它的'''''和'Attributes'屬性 - 都是公共的。您還可以爲您的自定義屬性描述符創建重載構造函數,該構造函數接受'MemberDescriptor'並在其中傳遞'DataColumnPropertyDescriptor'的實例 –

相關問題