在运行时用新创建的类型替换现有的类定义 [英] Replace existing class definition at runtime with newly created type

查看:131
本文介绍了在运行时用新创建的类型替换现有的类定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Demorepo

https://github.com/gabbersepp/csharp-dynamic-replace-class

使用方法


  1. 结帐

  2. 编译

  3. 删除TestLib.dll&来自console / bin / Debug的TestLib.pdb

  4. 通过cmd执行console.exe

  1. Checkout
  2. Compile
  3. Delete TestLib.dll & TestLib.pdb from console/bin/Debug
  4. Execute console.exe through cmd

首先阅读

我想在工作中实现一些目标,我认为这将是最好的解决方案。因此,请不要讨论我是否可以用其他方法解决此问题。如果我想讨论这个问题,我将创建一个新的SO帖子。

I have something in mind I want to achieve at work and I think that this would be the best solution. So please do not discuss if I can solve this with another way. If I would like to discuss about this, I will create a new SO post.

给定

库中的类:

namespace Test.TestLib
{
    public class Class1
    {
    }
}

会创建一个实例:

namespace console
{
    public class AnotherClass
    {
        public void Create()
        {
            new Class1();
        }
    }
}

并调用一个控制台应用创建

And a console app that calls create:

    static void Main(string[] args)
    {
        //...
        new AnotherClass().Create();
    }

请记住,只有 Class1 在一个额外的库中。其他两个类是相同的。

Please keep in mind that only Class1 is in an extra lib. The other two classes are in the same.

我想做什么

在运行时替换类型 Class1

        AssemblyName dynamicAssemblyName = new AssemblyName("TestLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
        dynamicAssembly =
            AssemblyBuilder.DefineDynamicAssembly(dynamicAssemblyName, AssemblyBuilderAccess.Run);
        var dynamicModule = dynamicAssembly.DefineDynamicModule("TestLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
        var modelType = dynamicModule.DefineType("Test.TestLib.Class1", TypeAttributes.Class).CreateType();

这有效。如果我使用的是Activator: Activator.CreateInstance(modelType); 我会得到这种类型的新实例。

This works. If i am using the Activator: Activator.CreateInstance(modelType); I get a new instance of this type.

但是

到达具有 new Class1 的行时,出现异常抛出:

When the line with new Class1 is reached, an exception is thrown:


异常状态:System.MethodAccessException:Fehler beim
Versuch der Methode console.AnotherClass.Create(), auf Methode
Test.TestLib.Class1..ctor() zuzugreifen。 bei
console.AnotherClass.Create()

Unbehandelte Ausnahme: System.MethodAccessException: Fehler beim Versuch der Methode "console.AnotherClass.Create()", auf Methode "Test.TestLib.Class1..ctor()" zuzugreifen. bei console.AnotherClass.Create()

类似的东西:


未处理的异常:System.MethodAccessException:尝试访问方法 console.AnotherClass.Create()
时发生错误
方法 Test.TestLib.Class1..ctor()。
在console.AnotherClass.Create()

Unhandled exception: System.MethodAccessException: An error occurred while trying to access the method "console.AnotherClass.Create ()" method "Test.TestLib.Class1..ctor ()".     at console.AnotherClass.Create ()

问题

这可能吗?

侧注
从/中删除TestLib文件需要debug文件夹,因为否则不会引发 AssemblyResolve 事件(请参见完整示例的仓库)

Sidenote: The deletion of the TestLib files from the /debug folder is required because otherwise the AsemblyResolve Event (please see the repo for a full example) is not raised

推荐答案

TypeAttributes.Public 添加到DefineType,它将成功执行

检查此内容: https://github.com/aIjundi/csharp-dynamic-replace-class

add TypeAttributes.Public to DefineType, it will execute successfully
Check this: https://github.com/aIjundi/csharp-dynamic-replace-class

这篇关于在运行时用新创建的类型替换现有的类定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆