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

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

问题描述

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

  var domain = AppDomain.CurrentDomain; 

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

builder.Save(fileName);

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



所以我尽管关于使用单独的 AppDomain

  var domain = AppDomain.CreateDomain(Test ); 

...

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


系统。 Runtime.Serialization.SerializationException:在程序集'mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'中输入'System.Reflection.Emit.AssemblyBuilder'未标记为可序列化。


我不知道这是如何涉及到一个非当前的 AppDomain调用 DefineDynamicAssembly / code>。所有我在网上找到的主要是关于在单独的域中执行程序集。也许在这里我想做的只是太具体,太先进,甚至根本不推荐,但是这将使我能够测试我们所有的汇编操作代码。



解决方案

我通过执行其他AppDomain中的代码建议

  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中查找委托。


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);

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.

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

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

...

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

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.

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?

解决方案

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);
});

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天全站免登陆