Copy Assembly Code between 2 or more dlls

0

I have ByteCode in an Assembly. I want to Copy this Code to another Assembly. It wasnt easy but I get a good Copy at first glance. I Can Copy namespaces, classes, custom Attributes, fields and so on. But I have a problem with the Method Bodys.

I Know I can get the Code with:

byte[] ilCode = method.GetMethodBody().GetILAsByteArray();

Furthermore I know how to set the new method Body:

MethodBuilder methodBuilder = typeBuilder.DefineMethod(method.Name, method.Attributes, method.CallingConvention, method.ReturnType, param.ToArray());
methodBuilder.SetMethodBody(ilCode, method.GetMethodBody().MaxStackSize, sig.GetSignature(), exce, null);

Variables are defined as following:

  • method : MethodInfo //original Method
  • param : List //List of Parametertypes
  • exec : List // List of all Exception clauses
  • sig : SignatureHelper //not quite sure but something with locals

Now i have the following result:

First the Original Method:

 .method private hidebysig instance void  onTargetFloorReached() cil managed
{
  // Code size       12 (0xc)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldstr      "TargetFloorReached"
  IL_0006:  call       instance void ['Assembly-CSharp']BaseWeb::CallFunctionWithParameter(string)
  IL_000b:  ret
} // end of method Lift::onTargetFloorReached

And now, what i get on the other side:

.method private hidebysig instance void  onTargetFloorReached() cil managed
{
  // Code size       12 (0xc)
  .maxstack  8
  IL_0000:  ldarg.0
INVALID TOKEN: 0x70000001
  IL_0006:  call        [ERROR: INVALID TOKEN 0x0A00000D] 
  IL_000b:  ret
} // end of method Lift::onTargetFloorReached

I have tryed loading every Dependency of the original dll but that wont change anything.

The SetMethodBody Method is defined as:

public void SetMethodBody (byte[] il, int maxStack, byte[] localSignature, System.Collections.Generic.IEnumerable<System.Reflection.Emit.ExceptionHandler> exceptionHandlers, System.Collections.Generic.IEnumerable<int> tokenFixups);

There is no Information about the "tokenFixups".

What are these fixups? How can I get them? Or could the misstake be somewhere else?

Edit: It seems that only function calls are Invalid. If I could Identify the function from its Byte representation, I could fix this.

c#
dll
.net-assembly
ilgenerator
asked on Stack Overflow Feb 22, 2019 by Dropye • edited Feb 22, 2019 by Dropye

1 Answer

0

You should use existing tools like https://github.com/dotnet/ILMerge or https://github.com/gluck/il-repack that allow you to combine dlls and exe into one file.

As for loading the same class twice, if the assembly is already loaded in the app domain it will not be loaded twice even if the assembly version is different somehow (I believe). So I don't see how this is an issue.

See for loading duplicate assemblies: https://social.msdn.microsoft.com/Forums/vstudio/en-US/9748a274-0925-48a1-8dc7-3214ffe55ff9/prevent-duplicate-dll-from-loading-twice-ccli?forum=netfxbcl

answered on Stack Overflow Feb 22, 2019 by Hastaroth

User contributions licensed under CC BY-SA 3.0