public class BaseDto
{
    public int ID{ get; set; }
}
public class Client: BaseDto
{
     public string Surname { get; set; }
     public string FirstName{ get; set; }
     public string email{ get; set; }
}

PropertyInfo[] props = typeof(Client).GetProperties();

最佳答案

也许这样吗?

// this is alternative for typeof(T).GetProperties()
// that returns base class properties before inherited class properties
protected PropertyInfo[] GetBasePropertiesFirst(Type type)
{
    var orderList = new List<Type>();
    var iteratingType = type;
    do
    {
        orderList.Insert(0, iteratingType);
        iteratingType = iteratingType.BaseType;
    } while (iteratingType != null);

    var props = type.GetProperties()
        .OrderBy(x => orderList.IndexOf(x.DeclaringType))
        .ToArray();

    return props;
}

关于c# - 如何使用反射在派生类的属性之前获取基类的属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19995955/

10-12 12:43
查看更多