Why the inherited field is not of the final type

I have the following code

public abstract class Parent
{
    AnObject AProperty {get; set;}
}
public class ChildA : Parent { }
public class ChildB : Parent { }

When I access an instance ChildAthrough reflection, I see that its member AProperty has an DeclaringTypeequal Parent. Unfortunately, I would like to rely on reflection to determine who is ChildA and who is ChildB.

More context . I'm actually trying to connect APropertythrough NInject to the when clause so that it resolves differently depending on the actual type of the object being created. Here is a simplified example:

Kernel.Bind<AnObject>().ToConstructor(..).WhenAnyAncestorMatches(c =>
      c.Request.Target.Member
       .DeclaringType.IsAssignableFrom(typeof(ChildA))
Kernel.Bind<AnObject>().ToConstructor(..).WhenAnyAncestorMatches(c =>
      c.Request.Target.Member
       .DeclaringType.IsAssignableFrom(typeof(ChildB))

Questions :

  • Am I doing something wrong?
  • Should I set APropertyon abstractand override it on each ChildX?
  • Is it possible to get the actual type in my predicate WhenAnyAncestorMatches?
+4
1

, AnObject AProperty , .


: , , ( ).


, , WhenInjectedInto<> WhenAnyAncestorMatches. WhenInjectedInto<> , WhenAnyAncestorMatches, , :

var binding = Kernel.Bind<AnObject>().ToConstructor(..);

Func<IRequest, bool> whenInjectedIntoCondition = 
    binding.WhenInjectedInto<int>().BindingConfiguration.Condition;

binding.WhenAnyAncestorMatches(c => whenInjectedIntoCondition(c.Request));

, 100%, .

+1

All Articles