我需要从 MyClass 中获取属性列表,排除 'readonly' 属性,我能做到吗?
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
上有一些属性,指示其可读/可写性。 - Sam AxemyProperties.IsReadOnly
是 PropertyInfo[] 属性之一。 - MethodMan