如果您确实需要获取所需信息,例如 afield或properties values/ names,
你怎么能不使用System.Reflection呢?
public class anyClass
{
public const string UserID = "usrID",
UserName = "usrName";
}
所以它发生了,因为我真的很想知道是否可以避免使用reflection,
我进行了一些研究,在我看来,我的使命的正确术语是
类型自省 (此处不存在专用标签SO)
Introspection不应与reflection,
这更进一步,是程序操纵值的能力,
元数据,properties和/或运行时functions的。object
通过使用系统反射,我可以访问Fields- values,如本示例代码所示:
var ac = new anyClass();
public List<string> anyClassFieldsValuesList()
{
// sorry this was the one for the class above
return typeof(anyClass).GetFields()
.Select(f =>f.GetValue(ac).ToString()).ToList<string>();
// this would do for nested class
// return typeof(anyClass).GetNestedTypes()
.First(t => String.Compare(t.Name, "anyNested", true) == 0)
.GetFields()
.Select(f => f.GetValue(ac).ToString())
.ToList<string>();
}
.net 是否为我们提供了使用工具或其他方法以更便宜的方式获取信息的方法?