在非当前 AppDomain 上使用 DefineDynamicAssembly 动态创建程序集时出现异常 [英] Exception while dynamically creating assemblies using DefineDynamicAssembly on non-current AppDomain

查看:37
本文介绍了在非当前 AppDomain 上使用 DefineDynamicAssembly 动态创建程序集时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在集成测试中动态创建程序集,以便测试一些程序集操作类.如果我使用以下代码来创建测试程序集:

I want to dynamically create assemblies in the integration tests for the purpose of testing some assembly manipulation classes. If I use the following code to create the testing assemblies:

var domain = AppDomain.CurrentDomain;

var builder = domain.DefineDynamicAssembly(
    new AssemblyName(assemblyName),
    AssemblyBuilderAccess.Save,
    directory);

builder.Save(fileName);

然后一切运行正常,程序集在所需的位置创建,但作为其中的一部分,它们也被加载到当前的 AppDomain,这是我不想要的.

then everything runs ok, the assemblies are created in the required location, but as part of this they are also loaded to the current AppDomain, which I don't want.

所以我想使用单独的 AppDomain 创建程序集:

So I though about creating the assemblies using the separate AppDomain:

var domain = AppDomain.CreateDomain("Test");

...

但是运行代码会在 var builder = domain.DefineDynamicAssembly(...); 行抛出异常:

But running the code throws an exception at the line var builder = domain.DefineDynamicAssembly(...);:

System.Runtime.Serialization.SerializationException:程序集mscorlib,版本=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"中的类型System.Reflection.Emit.AssemblyBuilder"未标记为可序列化.

System.Runtime.Serialization.SerializationException: Type 'System.Reflection.Emit.AssemblyBuilder' in assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

我不知道这与在非当前 AppDomain 上调用 DefineDynamicAssembly 有何关系.我在网上找到的主要是关于在不同的域中执行程序集.也许我在这里尝试做的太具体、太高级甚至根本不推荐,但它使我能够测试我们所有的汇编操作代码.

I have no clue how this relates to calling DefineDynamicAssembly on a non-current AppDomain. All I find online is mostly about executing assemblies in separate domains. Perhaps what I'm trying to do here is just too specific, too advanced or even not recommended at all, but it would enable me to test all our assembly manipulation code.

有人能指出我正确的方向吗?

Can someone please point me in the right direction?

推荐答案

我通过执行其他 AppDomain 中的代码使其正常工作,例如 建议.

I got it working by executing the code in the other AppDomain, like suggested.

var appdomain = AppDomain.CreateDomain("CreatingAssembliesAndExecutingTests", null,
   new AppDomainSetup { ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase });

appdomain.DoCallBack(() =>
{
   var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("temp"), AssemblyBuilderAccess.Run);
   var module = assembly.DefineDynamicModule("DynModule");
   var typeBuilder = module.DefineType("MyTempClass", TypeAttributes.Public | TypeAttributes.Serializable);
});

请注意,您必须在 AppDomainSetup 上指定 ApplicationBase 才能在其他 AppDomain 中找到委托.

Remark you have to specify the ApplicationBase on the AppDomainSetup in order to find the delegate in the other AppDomain.

这篇关于在非当前 AppDomain 上使用 DefineDynamicAssembly 动态创建程序集时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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