2015-10-13 75 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'的实例 –

相关问题