繁体   English   中英

如何在某个订单中获取类中的属性名称列表

[英]How to get a list of property names in a class in certain a order

我有一个类似于的类:

public class MyClass : MyBaseClass
{
        public string Field1 { get; set; }
        public string Field2 { get; set; }
        public string Field3 { get; set; }
        public string Field4 { get; set; }
}

public class MyBaseClass
{
        public string BaseField1 { get; set; }
        public string BaseField2 { get; set; }
        public string BaseField3 { get; set; }
        public string BaseField4 { get; set; }
}

然后我创建了一个从类中提取名称的方法。

private void MyMethod<T>(List<T> listData) where T : class
{
    String[] fieldNames = Array.ConvertAll<PropertyInfo, String>(typeof(T).GetProperties(), delegate(PropertyInfo fo) { return fo.Name; });

    // Do something with the fieldNames array....
}

所以当我得到我的数组时,它将按以下顺序:

Field1
Field2
Field3
Field4
BaseField1
BaseField2
BaseField3
BaseField4

我想知道是否可以更改顺序,以便基类字段首先跟随派生类字段?

让我们实现一个简单的方法来获得类层次结构中类的深度

null <- object <- ... <- MyBaseClass <- MyClass <- ...

履行

// 0     - null
// 1     - object
// ...
// n     - MyBaseClass
// n + 1 - MyClass
// ...
private static int TypeLevel(Type type) {
  if (null == type)
    return 0;

  return TypeLevel(type.BaseType) + 1;
}

然后在这个标准的Linq帮助下,唯一的小技巧是使用DeclaringType - DeclaringType属性的位置(在哪个类中):

// fieldNames are actually properties' names
string[] fieldNames = typeof(MyClass)
  .GetProperties()
  .OrderBy(p => TypeLevel(p.DeclaringType)) // <- base first, derived last
  .ThenBy(p => p.Name) // <- let's organize properties within each class
  .Select(p => p.Name) 
  .ToArray();

Console.Write(string.Join(Environment.NewLine, fieldNames));

结果:

BaseField1
BaseField2
BaseField3
BaseField4
Field1
Field2
Field3
Field4

最后,您的方法可以是这样的:

// we don't want any restictions like "where T : class"
private void MyMethod<T>(List<T> listData) {
  ...
  string[] fieldNames = typeof(T)
    .GetProperties()
    .OrderBy(p => TypeLevel(p.DeclaringType)) // <- base first, derived last
    .ThenBy(p => p.Name) // <- let's organize properties within each class
    .Select(p => p.Name)
    .ToArray();

  ...
}

您将不得不编写如下代码来遍历类层次结构,然后获取属性

static void GetProperties(Type type, List<string> returnValue) {
    var props = type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
    returnValue.AddRange(props.Select(p => p.Name).OrderBy(p => p));
}

private void MyMethod<T>(List<T> listData) where T : class {
    var type = typeof(T);
    List<string> properties = new List<string>();
    while (type != null) {
        GetProperties(type, properties);
        type = type.BaseType;
    }
}

选择基类的所有属性,并使用派生类的所有属性进行联合。 Distinct()将删除所有重复项。 如果需要更多复杂性,您将不得不尝试@Vikhram解决方案。

        var properties = typeof(MyBaseClass).GetProperties().Select(x => x.Name)
            .Union(typeof(MyClass).GetProperties().Select(x => x.Name)).Distinct();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM