【发布时间】:2013-03-04 13:52:39
【问题描述】:
我需要从 MyClass 中获取属性列表,不包括“只读”属性,我可以获取它们吗?
public class MyClass
{
public string Name { get; set; }
public int Tracks { get; set; }
public int Count { get; }
public DateTime SomeDate { set; }
}
public class AnotherClass
{
public void Some()
{
MyClass c = new MyClass();
PropertyInfo[] myProperties = c.GetType().
GetProperties(BindingFlags.Public |
BindingFlags.SetProperty |
BindingFlags.Instance);
// what combination of flags should I use to get 'readonly' (or 'writeonly')
// properties?
}
}
最后,我能把它们排序吗?我知道添加 OrderBy,但是怎么做呢?我只是在使用扩展。 提前致谢。
【问题讨论】:
-
PropertyInfo上有几个属性表明可读/写能力 -
myProperties.IsReadOnly是 PropertyInfo[] 属性之一 -
仅供参考:BindingFlags.SetProperty 在这种情况下不会做任何事情。
标签: c# reflection properties readonly writeonly