How to copy Type from one Assembly to Another

742 views
Skip to first unread message

trul...@gmail.com

unread,
Nov 15, 2011, 12:09:55 PM11/15/11
to mono-cecil
Hi,

I've been reading https://github.com/mono/cecil/tree/master/merge/Mono.Merge
and I'm close to understanding it but I feel im missing something
rather simple...

How can I copy a type from one assembly to another, currently I'm
doing this:

var t = assemblyDefA.MainModule.GetType("some name");
assemblyDefB.MainModule.Types.Add(t);

but I get "Type already attached".

How can I achieve this?

Thanks!

Jb Evain

unread,
Nov 16, 2011, 10:07:10 AM11/16/11
to mono-...@googlegroups.com
Hi,

(Sadly?) There's no easy way to move one type from a module to
another, as it involves taking a lot of decisions about what to do
with references. Also Mono.Merge is completely dead.

I suggest you look at http://code.google.com/p/il-repack/

Jb

gold...@gmx.de

unread,
Nov 16, 2011, 2:32:36 PM11/16/11
to mono-...@googlegroups.com
Jb Evain wrote:
> On Tue, Nov 15, 2011 at 6:09 PM, trul...@googlemail.com
> <trul...@gmail.com> wrote:
>> How can I copy a type from one assembly to another, currently I'm
>> doing this:
>>
>> var t = assemblyDefA.MainModule.GetType("some name");
>> assemblyDefB.MainModule.Types.Add(t);
>>
>> but I get "Type already attached".

Basically, you can't attach an existing instance (type, method, etc.)
that has already been attached to one assembly to a new assembly. You
have to clone the object instead, (deep-)copying its full state, which
is what we do in il-repack. The cloning methods are *not* part of cecil
itself: like Jb wrote, it involves taking decisions about the references
and we know these decisions in il-repack (where we know which types to
merge and which not to).

Simon

Michael DeMond

unread,
Nov 28, 2014, 1:54:23 PM11/28/14
to mono-...@googlegroups.com
Hello,

Has there been any improvements/suggestions around this since this last thread post, by chance?  I am dynamically creating an assembly via Roslyn, then reading it into Cecil with a MemoryStream via AssemblyDefinition.ReadAssembly.  When I Remove the Type from its original (Roslyn) module definition, and then Add it to the created Cecil module definition, it does work, but I do end up getting a "Specified method is not supported." exception when the module writes out.

Thanks for any insight you can provide.  I have a sinking feeling that I think I'm just going to have to learn IL after all. :P
Michael

Michael DeMond

unread,
Nov 28, 2014, 5:34:44 PM11/28/14
to mono-...@googlegroups.com
Oh man... I ended up just hacking away at the ILRepack code and just pulling over the Import functionality to get what I needed.  It works like a champ, now!  I would use the ILRepack NuGet, but it doesn't support assemblies that are in memory.  

I now have that capacity to compile a full syntax tree with Roslyn, read it as a binary format with Cecil, and then import the generated types in a Fody Weaver on build via the pulled in code from ILRepack... all without touching a line of IL.  I'm such a cheating hack, I know. :D  But it's super nice now as I will be able to define AOT decorators for my Xamarin.Forms applications (100% cross-platform compatible)... in C#, no less. SWEEEEEEET.

The one thing I will have to look into is the performance/time with the Roslyn compile... it's pretty chunky at ~800ms.

Once I have pushed/synced my code I will share here just in case anyone is interested in the future.

Thanks again for all the amazing work in creating Cecil!!!

--
--
--
mono-cecil
---
You received this message because you are subscribed to a topic in the Google Groups "mono-cecil" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mono-cecil/Yt9xQedjw54/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mono-cecil+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jb Evain

unread,
Nov 29, 2014, 5:16:54 AM11/29/14
to mono-...@googlegroups.com
Hey Michael,

That sounds like something you should write about for others to see.

Jb

On Fri, Nov 28, 2014 at 11:34 PM, Michael DeMond
> You received this message because you are subscribed to the Google Groups
> "mono-cecil" group.
> To unsubscribe from this group and stop receiving emails from it, send an

Michael DeMond

unread,
Nov 29, 2014, 10:12:04 AM11/29/14
to mono-...@googlegroups.com
OK, I am in the middle of fixing an issue with Fody, but I have published this to a fork in the meantime:


There you can see Roslyn + Cecil + Fody integration.  Like I mentioned, there is an issue currently when building this solution, so the code has to be considered "look but don't touch" for now. :)  Here is the issue if you'd like to follow along:

Thank you,
Michael
Reply all
Reply to author
Forward
0 new messages