C# TypeDescriptor.GetProperties来自另一个类中的属性
我正在使用GetProperties获取带有反射的属性文件C# TypeDescriptor.GetProperties来自另一个类中的属性,c#,C#,我正在使用GetProperties获取带有反射的属性文件 PropertyDescriptor descriptor = TypeDescriptor.GetProperties(GetType())[attrNane]; 但是是否可以从另一个类中获取PropertyDescriptor 范例 class a { public string name; } class b { public b() { PropertyDescriptor descriptor = TypeDesc
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(GetType())[attrNane];
但是是否可以从另一个类中获取PropertyDescriptor
范例
class a {
public string name;
}
class b {
public b() {
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(GetType())["a.name"];
}
}
因此,我想从类a
中的属性名“name”
中获取PropertyDescriptor
。有可能吗?您可以使用获取另一个类的类型
class a {
public string name {get; set;}
}
class b {
public b() {
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(typeof(a))["name"];
}
请注意,我将a.name
更改为属性。name
在本例中不是属性,而是一个内部字段。请注意,如果您仅在字符串中包含类型名称,但您知道它来自哪个程序集,则需要使用assembly.GetType,类似地,如果知道程序集名称是字符串,则需要在AppDomain.Current中查找该程序集