简体   繁体   English

如何在 dotnet 控制台应用程序中引用和使用同一程序集的多个版本?

[英]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:假设我的解决方案中有以下程序集文件参考:

  1. a.dll一个.dll
  2. b.dll b.dll
  3. c.dll dll
  4. Main_43.dll Main_43.dll
  5. a_40.dll a_40.dll
  6. b_40.dll b_40.dll
  7. c_40.dll c_40.dll
  8. Main_40.dll Main_40.dll

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.

相关问题 如何获得同一应用程序的多个版本以使用程序集的同一版本? C# - How can I get multiple versions of the same application to use the same version of an assembly? c# 引用具有多个配置版本的部件 - Reference an assembly with multiple configuration versions ASP.NET:多个程序集版本,相同的Web应用程序 - ASP.NET: Multiple assembly versions, same web application 如何动态加载引用同一程序集不同版本的DLL? - How to dynamically load DLLs which reference different versions of same assembly? 如何在 ASP.NET 应用程序中使用同一程序集的两个版本? - How do I use two versions of the same assembly inside an ASP.NET application? 加载同一程序集的多个版本 - Loading multiple versions of the same assembly 同一引用程序集的多个版本 - Multiple versions of same referenced assembly 引用同一程序集的多个版本 - Referencing multiple versions of the same assembly 如何在应用程序中使用两个dll引用相同dll的不同版本? - How to use two dlls in an application which reference different versions of same dll? DotNet Core 控制台应用程序:应用程序依赖项清单中指定的程序集 - DotNet Core console app: An assembly specified in the application dependencies manifest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM