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()
Reza
  • 18,865
  • 13
  • 88
  • 163
  • I would say that that is probably because your class `B` doesn't implement `Load()` – Kevin Mar 29 '16 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? –  Mar 29 '16 at 20:56
  • @KevinWells I have implemented the Load method in my class, see edited question – Reza Mar 29 '16 at 20:58
  • 1
    https://dotnetfiddle.net/ZL8U1N –  Mar 29 '16 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 – Kevin Mar 29 '16 at 21:03
  • Have you looked at the other questions on SO that pertain to this error message? Such as [this](http://stackoverflow.com/questions/9837002/interface-does-not-contain-a-definition-for-method)? – Kevin Mar 29 '16 at 21:06
  • @Will I am using Unity to resolve type, in your fiddle there is no `microsoft unity` – Reza Mar 29 '16 at 21:12
  • @KevinWells Yes, they are not related to `Unity` – Reza Mar 29 '16 at 21:13
  • Please post how your registering IB and how your retrieving an instance of it. – mxmissile Mar 29 '16 at 21:19
  • @RezaRahmati How certain are you that your issue is related to `Unity`? If you aren't sure, then maybe you should check on the things that fixed it for the other question, and see if that is the actual underlying problem – Kevin Mar 29 '16 at 21:22
  • 1
    @mxmissile See the updated question – Reza Mar 29 '16 at 21:28
  • Something is still not right, I just copy/pasted all your code and it ran as expected, no Exception. I'm using Unity v4.0 and .NET 4.6.1 – mxmissile Mar 29 '16 at 21:31
  • @mxmissile I ma using Unity2.1.505 and .Net4.5.2 – Reza Mar 30 '16 at 12:33

1 Answers1

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

Dmitri M
  • 517
  • 6
  • 13