Because of this issue here, I\'m trying to write a custom JsonConverter that handles cases where you subclass a list or a collection, then add extra properties to it. As such,
You have to select all members in MySubClass
and keep only those where DeclaringType == MySubClass
.
With LINQ, something like that (overkill) :
MemberInfo[] notInherited = GetType("MySubClass").GetMembers().Where(m => m.DeclaringType == GetType("MySubClass"));
Or with GetMembers()
overload :
MemberInfo[] notInherited = GetType("MySubClass").GetMembers(BindingFlags.DeclaredOnly);
While calling "GetMembers" method to get the members of the Type, you can specify "DeclaredOnly" in binding flag.
A lot of reflection functions accept a parameter of type BindingFlags. This enumeration includes a value DeclaredOnly:
Specifies that only members declared at the level of the supplied type's hierarchy should be considered. Inherited members are not considered.
MemberInfo.DeclaringType should do what you need. To get members directly defined in type X filter the members by DeclaringType == typeof(X)
.