相关疑难解决方法(0)

563
推荐指数
9
解决办法
47万
查看次数

使用C#反射从类T获取属性列表

我需要从类属性的列表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)

c# properties system.reflection type-parameter

3
推荐指数
1
解决办法
1788
查看次数