The type MyClass cannot be assigned to variables of type IMyInterface.Parameter name: from

Mar 5, 2012 at 7:28 AM
Edited Mar 5, 2012 at 7:30 AM


I have a problem in assigning an instance of a class(MyClass in a different assembly and implements IMyInterface) to an interface(IMyInterface). I am trying to drive the complete operation through the config file.

Currently the config file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
      <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, 
               Microsoft.Practices.Unity.Configuration" />
         <!-- Lifetime manager types -->
         <typeAlias alias="singleton"
             Microsoft.Practices.Unity" />
         <typeAlias alias="external"
             Microsoft.Practices.Unity" />
         <container name="containerOne">
            <register type="WinFormApp.IMyInterface, WinFormApp" mapTo="ClassLib.MyClass, ClassLib" />
And the code i am using to resolve is as follows:
            UnityContainer myContainer = new UnityContainer();
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            section.Configure(myContainer, "containerOne");

            IMyInterface obj = myContainer.Resolve<IMyInterface>();
Note: the functionality works when the Interface and the Class that implements the interface 
are in the same assembly.But failing when in different assembly. Error i am receving is 
"The type MyClass cannot be assigned to variables of type IMyInterface. Parameter name: from"
Additional Info: I am using Unity 2.0
Thanks in advance.
Mar 5, 2012 at 4:09 PM

Unity should have no issues resolving classes and interfaces from a variety of assemblies.  In fact, it is working properly in my test project.

It's hard to tell but what I think is happening is that you have been doing some refactoring and MyClass is not actually inheriting from IMyInterface at runtime.

The interface, IMyInterface, is in the WinFormApp assembly and the implementation class is in the ClassLib assembly.  This means that ClassLib will have to have a reference to WinFormApp in order to compile.  If it doesn't then that means it is not implementing the correct IMyInterface (and the code should be changed).  

I would guess that it probably does, however. If that is the case then that means that the WinFormApp cannot have a reference to ClassLib because there would be a circular reference.  If that is the case then your ClassLib assembly will not be automatically copied to the WinFormApp bin directory (unless you manually do it or have a post build command).  There is probably an old version of the ClassLib.dll in the WinFormApp bin directory that contains a version of MyClass that is not implementing WinFormApp.IMyInterface.

I would check all of your references and delete all of the files from your WinFormApp bin directory to start fresh. 

Also, I would consider moving the interface(s) to another common assembly to avoid these potential circular references.

Randy Levy
Enterprise Library support engineer 

Mar 6, 2012 at 9:23 AM

Hi Randy,

Yes, it was a silly mistake from my side the binary in the directory was not up to date as a result it was not able to resolve.

And thanks for all the suggestions, this was indeed a test project to analyse Unity.