简体   繁体   English

C#反射接口类型和统一

[英]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. 我使用Unity和app.config在某些程序集中动态加载和运行方法。 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. 但是在主应用程序中,我需要引用IProcessing接口,这用处不大,因为如果我添加另一个处理程序集,我不想在主应用程序中引用所说的新程序集,即重建,重新测试和重新分发,我只想使用Unity解决并酌情致电。

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. 我尝试了typeof(type),type.GetType()和Type(type)无济于事,所有这些都会导致不同的编译器错误。

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? 因此,问题是我如何才能使用在Unity解析和加载的未引用程序集中实例化并运行进程所需的接口的字符串名称? I'm a little new to Unity and this is the first time using reflection so please go easy on me. 我对Unity有点陌生,这是第一次使用反射,所以请对我轻松一点。 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. 在整个系统中删除我的Unity驱动的代码将非常耗时。

Any input appreciated, thanks. 任何输入表示赞赏,谢谢。

If you already have the Type of the class, you could just use 如果您已经有了该类的类型,则可以使用

var processor = Activator.CreateInstance(type, param[] object args) as IProcessing;

Edit: or you could use "dynamic", if you are willing to take the performance penalty coming with that. 编辑:或者,如果您愿意承担随之而来的性能损失,则可以使用“动态”。 And if you are ok with the fact, that you won't have any Intellisense help, during the writing of your code. 而且,如果您对此表示满意,那么在编写代码期间就不会得到任何Intellisense帮助。

var processor = Activator.CreateInstance(type, param[] object args) as dynamic;
processor.ExecuteProcess(parameterValues);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM