cil

Manually edit MSIL in compiled DLL

折月煮酒 提交于 2019-12-06 13:50:19
问题 If you want to make a small change to a .net application, is it possible to edit the MSIL directly? 回答1: Use ildasm.exe to decompile and recompile with ilasm.exe. For strong-named assemblies you will need the key file if you want that the recompiled assembly has the same identity as the original assembly. 来源: https://stackoverflow.com/questions/6746387/manually-edit-msil-in-compiled-dll

Are there tools to compile CIL to a binary?

冷暖自知 提交于 2019-12-06 08:49:14
问题 After seeing a previous post "Making Applications programmed in .NET languages work on older machines", I've got to wonder; Are there tools to compile CIL to a binary? If so, you could convert a CIL file to a native Windows app if you didn't want the user to require the .NET framework? Or even target the linux platform from .NET! That would be awesome! 回答1: Check this out: http://msdn.microsoft.com/en-us/library/6t9t5wcf.aspx And this: http://www.xenocode.com/Products/Postbuild-for-NET/

Signature of the body and declaration in a method implementation do not match

安稳与你 提交于 2019-12-06 06:33:10
问题 I want to implement an interface at runtime, however when i execute return Activator.CreateInstance(typeBuilder.CreateTypeInfo().AsType(), new RPCRequestProxy()); in RPCClientFactory.cs it throws System.TypeLoadException: Signature of the body and declaration in a method implementation do not match . I use dotnet core 1.0.4. The entry point Program.cs : namespace RPC { interface IRCPClient { Task Foo([UrlParam]int b, [UrlParam]string a, [UrlParam] DateTime c); } class Program { static void

Can you see the evaluation stack in mdbg?

我们两清 提交于 2019-12-06 05:09:39
Say I have the following CIL: ldc.i4 40 <- a breakpoint is set here ldc.i4.2 add box int32 call void [mscorlib]System.Console::WriteLine(string) I then use ilasm /debug main.il to assemble the exe and pdb files to use in mdbg. My question is, when I'm at say line 3, before the add operation is executed, I would want to see the evaluation stack which would show me the two int32 constants, 40 and 2, which I had loaded before. How can I view this evaluation stack in mdbg? I found the command that's needed to view the evaluation stack while debugging in mdbg: p[rint] . p[rint] prints local or

.NET 4.5 MethodBuilder.SetMethodBody

有些话、适合烂在心里 提交于 2019-12-06 03:56:17
问题 In the newest version of the .NET framework, version 4.5, the MethodBuilder class has a method called SetMethodBody that I believe is exactly what I'm looking at as an alternative to using ILGenerator (which is annoying and limited in odd ways). The documentation can be found here, although since .NET 4.5 is not out yet, it is not fully documented. I can supply all but two of the arguments, but the rest I will need help with. The first that I don't understand is byte[] localSignature , the

How to force compiler to use ldc.i8 IL instruction instead of ldc.i4 plus conv.i8?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 18:05:27
This is my C# code: const long pSize = 20; No matter if I use x64 or Any CPU to build it in Release mode, I get below MSIL instructions: IL_0010: ldc.i4.s 20 IL_0012: conv.i8 However, MSIL has ldc.i8 instruction. How to make compiler to use this? MSIL has ldc.i8 instruction. How to make compiler to use this? You don't. That would be worse code. ldc.i4.s 20 conv.i8 is three bytes of code: two for the instructions and one for the constant. Whereas ldc.i8 20 is nine bytes: one for the instruction and eight for the constant. The compiler is smart enough to avoid six unnecessary bytes. Remember,

Emit local variable and assign a value to it

僤鯓⒐⒋嵵緔 提交于 2019-12-05 16:56:50
问题 I'm initializing an integer variable like this: LocalBuilder a = ilGen.DeclareLocal(typeof(Int32)); How can I access it and assign a value to it? I want to do something like this: int a, b; a = 5; b = 6; return a + b; 回答1: Use the Ldloc and Stloc opcodes to read and write local variables: LocalBuilder a = ilGen.DeclareLocal(typeof(Int32)); LocalBuilder b = ilGen.DeclareLocal(typeof(Int32)); ilGen.Emit(OpCodes.Ldc_I4, 5); // Store "5" ... ilGen.Emit(OpCodes.Stloc, a); // ... in "a". ilGen.Emit

What is the point of nop in CIL

萝らか妹 提交于 2019-12-05 16:29:34
问题 So I wrote the following code in C#. class Test { int a; System.IO.StreamReader reader; public Test() { a = 5; reader = new System.IO.StreamReader(String.Empty); } } And the constructor of the class in IL looks like this .method public hidebysig specialname rtspecialname instance void .ctor() cil managed { // Code size 33 (0x21) .maxstack 8 IL_0000: ldarg.0 IL_0001: call instance void [mscorlib]System.Object::.ctor() IL_0006: nop IL_0007: nop IL_0008: ldarg.0 IL_0009: ldc.i4.5 IL_000a: stfld

How to declare a value type in CIL: `.class value` or just `.class`?

南楼画角 提交于 2019-12-05 14:21:46
I have taken a look at a C# struct FooStruct in ILDASM , and have seen the following: ILDASM here displays two differing declarations: one starting with .class value public (rear window & front window's title bar) one starting with just .class public (front window) And I wonder which syntax (if not both) is the correct one for declaring a value type? Is the value modifier strictly necessary, or optional, or a syntax error? Short answer: Value type definitions only require extends [mscorlib]System.ValueType ; the value attribute appears to be optional and has no apparent effect. I assume that

Inline MSIL/CIL

£可爱£侵袭症+ 提交于 2019-12-05 14:20:17
I created following simple method: public static void Main () { Console.WriteLine("Hello world!"); Console.ReadKey(true); } Then I used ILSpy to get the MSIL code: .method public hidebysig static void Main() cil managed { .entrypoint .maxstack 8 nop ldstr "Hello world!" call void [mscorlib]System.Console::WriteLine(string) nop ldc.i4.1 call valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey(bool) pop ret } Finally I tried to write the MSIL code into my C# code using the #if IL trick that I've found here . public static void Main () { #if IL nop ldstr "Hello world!"