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 will list the properties in this order: Surname, FirstName, email, ID
Want the properties to display in this order: ID, Surname, FirstName, email
Maybe this ?
// 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;
}
Not sure if there is a quicker way to do it, but first, get the type of the base type that you inherit from.
typeof(Client).BaseType
After that you can get only the base properties using bindingflags.
BindingFlags.DeclaredOnly
After that do the same for the Client type, and append the result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With