【发布时间】:2013-06-19 05:30:46
【问题描述】:
我正在尝试解决如何从类中获取所需属性的问题。我可以使用GetPropertiesForSerialization 之类的功能,但我想使用Attributes。我的想法是某些类属性将具有属性
class C{
[MyAttribute(1,picture)]
private string picture = "my.jpg";
[MyAttribute(2,Name)]
public string Name= "myName";
}
现在我想从其他班级获取属性
private void PrintAttributes(){
//Get list or other container with attibutes
var c = new C();
foreach( var item in C.GetMyAttributes()){
Console.WriteLine(item);
}
}
我知道如何获取函数等的属性,但不知道如何在不知道属性名称和保护(公共/私有)的情况下获取所有这些属性。
【问题讨论】:
-
您是否要获取所有属性名称?
标签: c#