简体   繁体   English

如何加载多个mscorlib.dll并分别调用它们的内部函数?

[英]How to load multiple mscorlib.dll and call their inner functions separately?

This is for research purpose. 这是出于研究目的。 I have .Net framework 2.0 installed, and I want to load mscorlib.dll 1.1 dynamically to execute its specific inner function. 我安装了.Net framework 2.0,我想动态加载mscorlib.dll 1.1来执行其特定的内部函数。

When I wrote this code in C#: 当我在C#中编写此代码时:

   static void Main(string[] args)
    {
        Assembly assem = Assembly.LoadFrom("C:\\mscorlib_my_private_1.1.dll");
        System.Type type = assem.GetType("System.Console");
        Type[] typeArray =new Type[1];
        typeArray.SetValue(typeof(string),0);
        System.Reflection.MethodInfo info = type.GetMethod("WriteLine", typeArray);
        object[] param = new object[1];
        param[0] = assem.FullName;
        type.InvokeMember("WriteLine",
        System.Reflection.BindingFlags.InvokeMethod, System.Type.DefaultBinder,
        "", param);
    }

The output is "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Not the expected 1.1 version . 输出为“mscorlib,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089”不是预期的1.1版本

After Googling a lot, I know that mscorlib.dll is very special, but is it impossible? 经过Google搜索后,我知道mscorlib.dll非常特别,但是不可能吗?

This is not possible: mscorlib.dll depends on internals of the .NET runtime it was written for. 这是不可能的:mscorlib.dll依赖于它为其编写的.NET运行时的内部。 These internals will probably have changed in later versions of the .NET runtime! 这些内部结构可能在.NET运行时的更高版本中发生了变化!

So even if you could somehow trick the .NET runtime into loading the wrong mscorlib.dll, you'd find that some methods work and others create all kinds of weird errors (behavioral changes, binding errors or access violations). 因此,即使您可以以某种方式欺骗.NET运行时加载错误的mscorlib.dll,您会发现某些方法有效,而其他方法会创建各种奇怪的错误(行为更改,绑定错误或访问冲突)。

You can't load any version of mscorlib other than the one for the .NET framework version you are running. 除了正在运行的.NET框架版本之外,您无法加载任何版本的mscorlib。 If you request another, you get the one that is already loaded. 如果您请求另一个,您将获得已加载的那个。 This is why mscorlib has strict backwards compatibility. 这就是为什么mscorlib具有严格的向后兼容性。

This strikes me as impossible. 这让我觉得不可能。 What you could maybe do, though, is compile a program against every version of the .NET framework you want to target, and then run each of those programs from your main app. 但是,你可以做的是针对你想要定位的每个.NET框架版本编译一个程序,然后从你的主应用程序运行这些程序中的每一个。

.NET 2.0 does not allow to add a reference of mscorlib.dll of version 1.1. .NET 2.0不允许添加1.1版的mscorlib.dll引用。 So to load mscorlib of version 1.1 you can do one thing that you write a pre-build script to copy this DLL to your bin (Or target folder) and this way your code will be able to access both versions of mscorlib.dll. 因此,要加载版本1.1的mscorlib,您可以执行一项操作,即编写预构建脚本以将此DLL复制到bin(或目标文件夹),这样您的代码就能够访问两个版本的mscorlib.dll。 eg. 例如。 Copy $(Location of mscorlib.dll)\\mscorlib.dll $(TargetDir) will copy this file to output location of your project. 复制$(Location of mscorlib.dll)\\mscorlib.dll $(TargetDir)会将此文件复制到项目的输出位置。

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

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