获取子类的属性 [英] Get properties of sub class

查看:85
本文介绍了获取子类的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的课

I have class like this

public class Person
{
   public string Id{get;set;}
   public string Name{get;set;}
}

我正在将此Person类继承为Contact类

I'm inheriting this Person class to the Contact class

public class Contact : Person
{
  public string Address{get;set;}
}

我正试图从另一个类中获取像这样的联系人类的属性

From another class I'm trying to get the properties of the contact class like this

propertydescriptorcollectionpc = TypeDescriptor.GetProperties(typeof(Contact));

但是,这仅给出了超类的属性.如何将子类的属性获取到propertydescription集合.

But this gives only the super class's properties only. How can I get the sub class's properties to a propertydescription collection.

推荐答案

使用 Type.GetProperties 代替,它也可以选择返回基本类型的属性. TypeDescriptor设计用于与类型本身之外的组件一起使用 会影响结果.

Use Type.GetProperties instead which can optionally return properties of base types as well.  TypeDescriptor is designed more for use with components where things outside the type itself can impact the results.

迈克尔·泰勒
http://msmvps.com/blogs/p3net

Michael Taylor
http://msmvps.com/blogs/p3net


这篇关于获取子类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆