0

I have this classes and interfaces

public interface IA{
  void Load();
}

public interface IB : IA{
}

public class B : IB{
   public void Load(){
      //some code
   }
}

and I register the IB for type B

Microsoft Unity resolves IB to correct type which is B, but when I try to call Load it shows an error IB does not contain a definition for 'Load'

Update

This is my unity configuration

var unityContainer = new UnityContainer();

unityContainer.RegisterType<IB, B>();

var obj = unityContainer.Resolve<IB>();
obj.Load()
13
  • I would say that that is probably because your class B doesn't implement Load() Commented Mar 29, 2016 at 20:56
  • That's not a unity issue, and your code above won't even compile. You're doing something else weird. What are you actually doing? Commented Mar 29, 2016 at 20:56
  • 1
    dotnetfiddle.net/ZL8U1N Commented Mar 29, 2016 at 21:02
  • 2
    It seems that as Will says, you are still leaving out some details. I appreciate the attempt to cut out the unnecessary code for the sake of brevity, but I am guessing that you are cutting out something important that we can't see from the minimal code you are including Commented Mar 29, 2016 at 21:03
  • 1
    @mxmissile See the updated question Commented Mar 29, 2016 at 21:28

1 Answer 1

1

IB does not contain definition for Load() but IA does, so you either have to resolve IA to B via unity or once you resolve IB, cast it to IA.

Update 1: I have to agree with other folks here, that the OP code does work and resolve correctly with Unity (tested with .NET 4.5.2 and Unity 2.1.505.2). I am not sure why my suggestion above solved the OP issue, so there has to be some details omitted in the original question.

Update 2: Working Fiddle based on @will's https://dotnetfiddle.net/FoYQSM

Sign up to request clarification or add additional context in comments.

6 Comments

So Unity does not honor IB inheriting IA?
This solves the problem, but I am wondering how the unity doesnot honor the inheritance in Interfaces
Are the interfaces in the same assembly and do you do any dynamic registration for your container?
ffs, unity just instantiates types for you. It cannot "respect" or not "respect" the implementation of a type. Yall need help.
@Will There is a Your Answer text box below... use it!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.