反射的一个问题,关于取得父类中的property

zhangfire 2008-07-22 01:12:23
在一个Interface中,定义了一个 string Name{get;set;}的property,现在有两个class
class1, class2
其中class1显式实现了这个interface,class2则继承了class1.
完全不知道这个interface以及class1,class2的情况下,拿到了一个class2的对象.
请问怎样获得所有的property,包括父类中的这个叫Name的.
希望答案是通用的,千万不要是针对于这个interface以及class1, class2的.
...全文
270 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangfire 2008-07-22
  • 打赏
  • 举报
回复
其实怪我,这个问题是两部分:
1. 拿到所有显式实现了Interface的父类中定义的Property
2. 不能拿重了,隐式实现的时候,会导致拿重的问题.

有很多比较丑陋的办法是可以实现的,我想问的是有没有比较好的办法可以实现拿全了,并且不重.
gaoqi9902 2008-07-22
  • 打赏
  • 举报
回复


static void Main(string[] args)
{

Class2 c2 = new Class2();

PropertyInfo[] pis = c2.GetType().GetProperties();


}
gaoqi9902 2008-07-22
  • 打赏
  • 举报
回复

static void Main(string[] args)
{

Class2 c2 = new Class2();

PropertyInfo[] pis = c2.GetType().GetProperties();


}
ojekleen 2008-07-22
  • 打赏
  • 举报
回复
Type t=c2.gettype().basetype;
foreach (PropertyInfo p in t.Getproperties())
{
Console.WriteLine(p.Name);
}
ojekleen 2008-07-22
  • 打赏
  • 举报
回复
class1显示实现interface,class2是没有Name属性的,你利用this.base来获取class2的父类class1也是不能通过IDE获取该属性的。
反射解决方案大致如下:(没有编译器,手误见识)

//以c2为你拿到了的class2对象
Type t=c2.gettype().basetype;
PropertyInfo[] ps = t.GetProperties();
foreach (PropertyInfo p in ps){
//这里面应该可以循环出来显示实现接口的属性}
zhangfire 2008-07-22
  • 打赏
  • 举报
回复
Interface MyInterface
{
int Age
{
get;
set;
}

string Name
{
get;
set;
}
}

Class Class2 : MyInterface
{
public int Age
{
get...
set...
}

public string Name
{
get...
set...
}
}


用上述的办法,将会从Class2上得到Name和Age,会从MyInterface上得到Name,Age
上山老人 2008-07-22
  • 打赏
  • 举报
回复
不知道 来学习
fuadam 2008-07-22
  • 打赏
  • 举报
回复
Age属性,你类里面的?
zhangfire 2008-07-22
  • 打赏
  • 举报
回复
比如interface中有Name,Age, Class2隐式实现了这个interface
我直接取所有的class2上的property(public & non public),会得到包括Name以及Age的,那么再通过所有的Interface去得到Property,会得到Name和Age这两个property,虽然通过Class2和通过Interface得到的PropertyInfo是不一样的,实际对我的需求来说,重复的Name和Age是一样的.

[Quote=引用 6 楼 fuadam 的回复:]
你说的重复是什么意思
[/Quote]
fuadam 2008-07-22
  • 打赏
  • 举报
回复
你说的重复是什么意思
zhangfire 2008-07-22
  • 打赏
  • 举报
回复

怎么保证不重复呢?

[Quote=引用 3 楼 pupo 的回复:]
Type的GetInterfaces方法,BaseType属性
[/Quote]
fuadam 2008-07-22
  • 打赏
  • 举报
回复
GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)

你隐式实现查出来的也是一条
pupo 2008-07-22
  • 打赏
  • 举报
回复
Type的GetInterfaces方法,BaseType属性
zhangfire 2008-07-22
  • 打赏
  • 举报
回复
可能是我没说全,全部的property找出来只是条件一,这里是不应该有重复的情况.如果class2隐式实现了一个interface,结果就会有重复.

[Quote=引用 1 楼 Macosx 的回复:]
MyInterface c2 = new class2();
foreach (var item in c2.GetType().GetProperties())
{
Console.WriteLine(item.DeclaringType + "." + item.Name);
}

foreach (var intrfc in c2.GetType().GetInterfaces())
{
foreach (var item in intrfc.GetProperties())
{

[/Quote]
Macosx 2008-07-22
  • 打赏
  • 举报
回复
MyInterface c2 = new class2();
foreach (var item in c2.GetType().GetProperties())
{
Console.WriteLine(item.DeclaringType + "." + item.Name);
}

foreach (var intrfc in c2.GetType().GetInterfaces())
{
foreach (var item in intrfc.GetProperties())
{
Console.WriteLine(item.DeclaringType + "." + item.Name);
}
}

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧