Unity to resolve types from external assembly ( without referencing it)

Sep 22, 2009 at 8:56 PM

Hello,

I'm working on an expanding project that we chose to integrate with Unity.Today I tried to enhance Unity to be able to resolve types from external assemblies ( Plugins ) without them being referenced. I managed to do that with the help of the MEF framework together with Unity. But my code is rather smelly ( for release purposes )

I had to modify UnityTypeResolver class from namespace Microsoft.Practices.Unity.Configuration ( to be more specific, ResolveType method)

It became :

public Type ResolveType(string typeName)
        {
            UnityTypeAlias typeAlias = typeAliases[typeName];

            if (typeAlias == null)
            {
                try
                {
                    return Type.GetType(typeName, true);
                }
                catch(FileNotFoundException)
                {
                    //we catch the exception that means that the specific type has not been
                    //loaded because it hasn't been found
                    //Of course,this behaviour is perfectly normal if you come to think about it
                    //we are talking about an unreferenced assembly

                    Type pluginnableType = GetTypeFromPlugins(typeName); // this calls MEF and search for plugins and tries to resolve types that implement my given contract interface
                    if (pluginnableType == null)
                        throw;
                    else
                    {
                        return pluginnableType;
                    }
                }
            }

            return typeAlias.Type;
        }

 

 

It now works beautifully but because this solution is not feasible for release purposes,could you point me out to the right direction ? could i somehow inject my own UnityTypeResolver in the framework ? Or,if this is not possible yes could the next version suport such a thing?

 

Thanks,

Doru.

Sep 22, 2009 at 8:59 PM

To bring more detail, i am configuring Unity container from a config file , in which i resolve my contract with a type from the unreferenced assembly

Thanks,

Doru