Reputation: 779
Imagine you have abstract base class A
, and also
abstract class B
that inherites from A
+ overriding method of A named foo()
In addition, you have concrete class C
which inherites from B
C
has the overriden method foo
inherited.
Now consider method foo
is using reflection and iterates all over the class properties.
The question is : When C.foo()
is lunched, the reflection will be done on C Class
properties or on B class
properties?
I need it to be done on properties from level C
only.
Upvotes: 1
Views: 94
Reputation: 551
Let us suppose that your class A looks something similar to this.
abstract class A
{
public string PropertyA { get; set; }
public abstract List<PropertyInfo> Foo();
}
Now, should your class B inherit from this class, and override the Foo() method, then it might look something like the following definition.
abstract class B : A
{
public string PropertyB { get; set; }
public override List<PropertyInfo> Foo()
{
return GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.ToList();
}
}
Also, your last class, namely C, which is the only concrete class would then simply be defined as follows.
class C : B
{
public string PropertyC { get; set; }
}
In order to test this solution, and see that the only returned property is the PropertyC, you would need to run the following lines of code.
class Program
{
static void Main()
{
new C().Foo().ForEach(x => Console.WriteLine(x.Name));
Console.Read();
}
}
Upvotes: 0
Reputation: 3082
See BindingFlags.DeclaredOnly
:
public override void Foo() {
PropertyInfo[] piAry = GetType().GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly);
}
Upvotes: 1