从另一个组件添加类型定义 [英] Adding a TypeDefinition from another Assembly

查看:148
本文介绍了从另一个组件添加类型定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一个是快把我逼疯了。

This one is driving me crazy.

AssemblyDefinition asm1 = AssemblyDefinition.ReadAssembly(example);
AssemblyDefinition asm2 = AssemblyDefinition.ReadAssembly(example2);
asm2.MainModule.Types.Add(asm1.MainModule.Types[0]);



每当我试着执行上面的代码中,我得到这个错误
'已经连接类型我决定看看这个错误在MonoCecil来源,我发现,因为该类型的MainMoudle不是ASM2 MainModules它抛出这个错误。所以我决定复制类型到一个新的。

Whenever I try to execute the above code I get this error 'Type already attached' I decided to look this error at MonoCecil source and I found it throws this error because the Type's MainMoudle isn't asm2 MainModules. So I decided to Copy that Type to a new one.

TypeDefinition type2 = new TypeDefinition("", "type2",  Mono.Cecil.TypeAttributes.Class);
foreach (MethodDefinition md in asm2.Methods )
{
        type2.Methods.Add(md);
}

和则该类型添加到我的组装正常,但,这将引发另一个错误,不支持指定的方法。。 ?
有什么想法,为什么我收到此错误

And then add this type to my assembly normally but this throws another error, 'Specified method is not supported.'. Any thoughts why I am getting this error?

编辑:我想补充,我想添加的类型包含它采用指针的一些方法。也许这是什么问题?据我所知,单声道支持,但不是混合模式。

Just to add, the type I'm trying to add contains some methods which uses pointers. Might this be the problem? As far as I know mono supports that but not mixed mode.

推荐答案

恐怕没有内置的,简单的方法来做到这一点。

I'm afraid there's no built in, easy way to do this.

当您阅读塞西尔组装,每一块的元数据是由元数据中定义的模块粘在一起,你不能简单地采取的方法从一个模块,并将其添加到另一个。

When you read an assembly with Cecil, every piece of metadata is glued together by the Module the metadata is defined in. You can't simply take a method from a module, and add it into another one.

要实现这一目标,您需要将MethodDefinition克隆到绑到其他模块MethodDefinition。同样,没有什么内建然而这一点。

To achieve this, you need to clone the MethodDefinition into a MethodDefinition tied to the other module. Again, there's nothing built-in yet for this.

我建议你看一看的 IL-重新包装,这是一个开源的ILMerge克隆。它准确的是,它需要从不同的模块类型,并将它们克隆到另一个。

I suggest you have a look at IL-Repack, which is an open-source ILMerge clone. It does exactly that, it takes types from different modules, and clone them into another one.

这篇关于从另一个组件添加类型定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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