我需要从类属性的列表T
- GetProperty<Foo>()
.我尝试了以下代码但它失败了.
样品类:
public class Foo {
public int PropA { get; set; }
public string PropB { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我尝试了以下代码:
public List<string> GetProperty<T>() where T : class {
List<string> propList = new List<string>();
// get all public static properties of MyClass type
PropertyInfo[] propertyInfos;
propertyInfos = typeof(T).GetProperties(BindingFlags.Public |
BindingFlags.Static);
// sort properties by name
Array.Sort(propertyInfos,
delegate (PropertyInfo propertyInfo1,PropertyInfo propertyInfo2) { return propertyInfo1.Name.CompareTo(propertyInfo2.Name); });
// write property names
foreach (PropertyInfo propertyInfo in propertyInfos) {
propList.Add(propertyInfo.Name); …
Run Code Online (Sandbox Code Playgroud)