[英]How to reference and use multiple versions of same assembly in dotnet console application?
Lets say i have the below assembly files reference in my solution:假设我的解决方案中有以下程序集文件参考:
My console application used to load only the first 4 before as the client used Main_43.dll which internally used a,b and c dll files.我的控制台应用程序以前只加载前 4 个,因为客户端使用 Main_43.dll,它在内部使用了 a、b 和 c dll 文件。 Now, i have to add a new client to my console application that will be using Main_40.dll which is the older version of Main_43.dll(I have to use the old version. No option to upgrade).
现在,我必须向我的控制台应用程序添加一个新客户端,该客户端将使用 Main_40.dll,它是 Main_43.dll 的旧版本(我必须使用旧版本。没有升级选项)。 I have renamed a, b and c dll files for this version by suffixing '_40' as seen above.
如上所示,我通过添加后缀“_40”来重命名此版本的 a、b 和 c dll 文件。
I have referenced all of them in my console application.我已经在我的控制台应用程序中引用了所有这些。 How can i tell the application to use Main_43 or Main_40 based on the parameter i pass to console app and how can i be sure that Main_40 will reference a,b and c dlls files that are meant for v40 and vice versa?
我如何根据传递给控制台应用程序的参数告诉应用程序使用 Main_43 或 Main_40,我如何确定 Main_40 将引用适用于 v40 的 a、b 和 c dll 文件,反之亦然?
Also, what is the best practice to call methods in my program that might me in both dlls?另外,在我的程序中调用可能在两个 dll 中都调用的方法的最佳实践是什么? Should i use the full reference name such as:
我应该使用完整的参考名称,例如:
Main_40.Export exportData = new Main_40.Export();
or is there a better way?或者,还有更好的方法?
The assembly files are strong named程序集文件是强命名的
So this is how i went about with the change.所以这就是我进行更改的方式。 I created a folder 'libs' under which i have 2 folders v40 and v43 and i have put the dlls 1 to 4 in v43 and the rest in v40 and then when my class is called, i load the dll dynamically.
我创建了一个文件夹“libs”,在其中我有 2 个文件夹 v40 和 v43,我将 dll 1 到 4 放在 v43 中,其余放在 v40 中,然后当我的类被调用时,我动态加载了 dll。 I also made sure that 'Copy always' is set so that dlls are always moved to the folder during build.
我还确保设置了“始终复制”,以便在构建过程中始终将 dll 移动到该文件夹中。
Now when i hit the client constructor that will be using v40 dll, i load the assembly path and then create an instance of main_40现在,当我点击将使用 v40 dll 的客户端构造函数时,我加载程序集路径,然后创建 main_40 的实例
protected const string mxMainDLLPath = @"libs\v40\Main_40.dll";
dynamic mainObj = null;
public ClientClass()
{
Assembly assembly = Assembly.LoadFrom(mxMainDLLPath );
Type T = assembly.GetType("Main_40.Export");
mainObj = Activator.CreateInstance(T);
}
Now to call any method in Main_40.dll, i can do mainObj.Export().现在要调用 Main_40.dll 中的任何方法,我可以执行 mainObj.Export()。
Thanks to @NightOwl888 for all the help.感谢@NightOwl888 的所有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.