mercredi 19 septembre 2018

C# Reflection Interface Type and Unity

hope someone can help. I use Unity and app.config to dynamically load and run methods in certain assemblies. In our main application I used to do something like ...

var ourProcess = container.Resolve<IProcessing>(new ResolverOverride[] { new ParameterOverride("parameter1", value1) });
ourProcess.ExecuteProcess(parameterValues); 
// parameterValues a string list, all of my processes use this main calling mechanism.
// there are many other interfaces i a number of other assemblies along with IProcessing

However in the main application I need a reference to the IProcessing interface and that's of little use because if I add another processing assembly I don't want to reference said new assembly in the main application, rebuild, retest and redistribute, I just want Unity to resolve and call as appropriate.

I've tried using reflection along the lines of ...

Assembly asm = Assembly.LoadFrom("CompName.Application.Processing.dll");
foreach (Type type in asm.GetTypes())
{
    if (type.GetInterface("IProcessing") != null)
    {
        var ourProcess = container.Resolve<type> (new ResolverOverride[] { new ParameterOverride("parameter1", value1) });
        oSummary = ourProcess.ExecuteProcess(parameterValues);
    }
}
// at runtime we will know the strings "CompName.Application.Processing.dll" and "IProcessing"

However at compile time we get the "'type' is a variable but is used like a Type", fair enough.

I've tried typeof(type), type.GetType() and Type(type) to no avail, all resulting in different compiler errors.

So, the question is how can I use the string name of the interface I require to instantiate and run a process in a non-referenced assembly resolved and loaded by Unity? I'm a little new to Unity and this is the first time using reflection so please go easy on me. I do feel though that this should be relatively easy and feel I am missing something fundemental.

I have searched for the previous few hours and tried one or two things but none have given me the results I need. It would be time consuming to remove my Unity driven code throughout the systems.

Any input appreciated, thanks.





Aucun commentaire:

Enregistrer un commentaire