I have a class that only defines some public properties but has a long chain of inheritance.
public class AgencyPaginatedQuery : SortedPaginatedQuery
{
public string Code { get; set; }
public string Name { get; set; }
public string Initials { get; set; }
}
public class SortedPaginatedQuery : PaginatedQuery
{
public string Sort { get; set; }
public string Order { get; set; }
}
public class PaginatedQuery
{
public int? Start { get; set; }
public int? Size { get; set; }
}
The class I am looking at is AgencyPaginatedQuery
and I am trying to get the names of said properties using LINQ and Reflection. So far I have been able to get which of these properties belong to the class in question with a simple query
var type = typeof(AgencyPaginatedQuery);
var names = from prop in type.GetProperties()
where prop.DeclaringType.Name == type.Name &&
prop.MemberType == System.Reflection.MemberTypes.Property
select prop.Name;
this brings me back
[0] = "Code"
[1] = "Name"
[2] = "Initials"
But now I need to filter from that list the properties that are overridden in case I write
public class AgencyPaginatedQuery : SortedPaginatedQuery
{
public string Code { get; set; }
public string Name { get; set; }
public string Initials { get; set; }
public override string Sort { get; set; }
}
this last property Sort
does not appear in the list.
How can I know if the property has a override
using my query?
PS: I know I have to mark the property in the base class as virtual for this to work.
I'll give you a way to check it by
GetBaseDefinition()
Using the flag
BindingFlags.DeclaredOnly
ignores inherited members.if the property is overridden
MethodInfo
the getter will be differentMethodInfo
fromGetBaseDefinition()
You could get it this way:
As a result I get: