some words about task. There is C# Class Library which consists of interfaces, and every interface has single implementation (class) in another C# Class Library. All these libraries compiles in separate assemblies.
//in interface library
public interface IRandom
//in implementation library
public class Random:IRandom
public int GetRandom()
var rand = new System.Random();
Mappings between interfaces and implementations stored in XML. And special class Resolver which exists in yet another library performs bindings via MS Unity.
And there is Console Application which references to interfaces and resolver libraries. It knows only about interfaces and it use Resolver class instance for getting appropriate implementation.
//in resolver library
public class Resolver
private readonly IUnityContainer _container;
_container = new UnityContainer();
//parsing XML, getting type, interfaces and assemblies names
//all names saved in mappings variable
//mappings type consists of simple members that are not defined here (AssemblyName, TypeName, ...)
foreach (var mapping in mappings)
var assembly = Assembly.LoadFrom(mapping.AssemblyName);
var iface = mapping.InterfaceName;
Type interfaceType = assembly.GetType(iface);
var moduleassembly = Assembly.LoadFrom(mapping.Mapping.AssemblyName);
var module = mapping.Mapping.TypeName;
Type moduleType = moduleassembly.GetType(module);
// this code works fine and all types and interfaces successfully registered
// in _container instance
public object Resolve(Type type)
At runtime i've got unity container error:
static void Main(string args)
var resolver = new Resolver();
IRandom random = (IRandom) resolver.Resolve(typeof (IRandom));
"Resolution of the dependency failed, type = "IRandom", name = "(none)". Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, IRandom, is an interface and cannot be constructed. Are you missing a type mapping?"
I am sure that IRandom has mapping. I've checked it in debugger.
The only reason why this error can appear is difference between IRandom type instances. One I get from dynamically loaded library (Assembly.LoadFrom()), another I get statically using typeof (IRandom).
Also I've compared these two type instances in debugger. Most of members including FullName, Assembly info and GUID are equal.
Is it possible to solve this problem using Unity? Thanks.