cil

Programmatic MSIL injection

房东的猫 提交于 2019-12-03 04:05:26
Let's say I have a buggy application like this: using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("2 + 1 = {0}", Add(2, 1)); } static int Add(int x, int y) { return x + x; // <-- oops! } } } The application is already compiled and deployed into the wild. Someone has found the bug, and now they are requesting a fix for it. While I could re-deploy this application with the fix, its an extreme hassle for reasons outside of my control -- I just want to write a patch for the bug instead. Specifically, I want to insert my own MSIL into

Implicit conversion to System.Double with a nullable struct via compiler generated locals: why is this failing?

只愿长相守 提交于 2019-12-03 03:10:13
问题 Given the following, why does the InvalidCastException get thrown? I can't see why it should be outside of a bug (this is in x86; x64 crashes with a 0xC0000005 in clrjit.dll). class Program { static void Main(string[] args) { MyDouble? my = new MyDouble(1.0); Boolean compare = my == 0.0; } struct MyDouble { Double? _value; public MyDouble(Double value) { _value = value; } public static implicit operator Double(MyDouble value) { if (value._value.HasValue) { return value._value.Value; } throw

Learning CIL [closed]

一曲冷凌霜 提交于 2019-12-03 03:02:02
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Does anybody know any good resources for learning how to program CIL with in-depth descriptions of commands, etc.? I have looked around but not found anything particularly good. Curt Hagenlocher The only CIL book on my shelf is Expert .NET 2.0 IL Assembler

Fast serialization and deserialization using dynamically emitted POCOs

落爺英雄遲暮 提交于 2019-12-03 01:53:53
I am currently serializing SQL table rows into a binary format for efficient storage. I serialize/deserialize the binary data into a List<object> per row. I'm trying to upgrade this to use POCOs, that will be dynamically generated (emitted) with one Field per column. I've been searching online for hours and have stumbled upon ORMs/frameworks like EF, T4, ExpandoObject, but all of these either use a dynamic object (properties can be added/removed on the fly) or simply generate a POCO before compiling. I cannot use templating because the schema of the tables is unknown at compile time, and using

Can C/C++ software be compiled into bytecode for later execution? (Architecture independent unix software.)

别等时光非礼了梦想. 提交于 2019-12-03 00:25:52
I would want to compile existing software into presentation that can later be run on different architectures (and OS). For that I need a (byte)code that can be easily run/emulated on another arch/OS ( LLVM IR? Some RISC assemby?) Some random ideas: Compiling into JVM bytecode and running with java. Too restricting? C-compilers available? MS CIL. C-Compilers available? LLVM? Can Intermediate representation be run later? Compiling into RISC arch such as MMIX. What about system calls? Then there is the system call mapping thing, but e.g. BSD have system call translation layers. Are there any

Why is it necessary to call :this() on a struct to use automatic properties in c#?

吃可爱长大的小学妹 提交于 2019-12-02 23:15:31
If I define a struct in C# using automatic properties like this: public struct Address { public Address(string line1, string line2, string city, string state, string zip) { Line1 = line1; Line2 = line2; City = city; State = state; Zip = zip; } public string Line1 { get; protected set; } public string Line2 { get; protected set; } public string City { get; protected set; } public string State { get; protected set; } public string Zip { get; protected set; } } When I attempt to build the file, I get a compilation error saying The 'this' object cannot be used before all of its fields are assigned

Why is it so easy to decompile .NET IL code?

∥☆過路亽.° 提交于 2019-12-02 22:50:28
Why is it so easy to decompile .NET IL-code into source code, compared to decompiling native x86 binaries? (Reflector produces quite good source code most of the time, while decompiling the output of a C++ compiler is almost impossible.) Is it because IL contains a lot of meta data? Or is it because IL is a higher abstraction than x86 instructions? I did some research and found the following two usefull articles, but neither of them answers my question. MSIL Decompiler Theory C Decompiler - Quick primer I think you've got the most important bits already. As you say, there's more metadata

How to use ICSharpCode.Decompiler to decompile a whole assembly to a text file?

岁酱吖の 提交于 2019-12-02 21:51:35
问题 I need to get either the whole IL Code or Decompiled Source Code to a text file. Is that possible with the ILSpy Decompile Engine ICSharpCode.Decompiler? 回答1: With ILSpy, you can select an assembly node in the tree view, and then use File > Save Code to save the result to disk. ILSpy will use the currently selected language to do so, so it can both disassemble and decompile. When decompiling to C#, the save dialog will have options for saving a C# project (.csproj) with separate source code

How to insert CIL code to C#

左心房为你撑大大i 提交于 2019-12-02 20:36:57
Is it possible to insert IL code to C# method? I just posted a utility which allows entire C# function bodies to be automatically replaced with inline IL, using a custom attribute. Like similar utilities, this works via the ILDASM/ILASM round-trip which can be set up as a post-build step. The tool also adjusts the PDB in order to preserve single-stepping and setting breakpoints on individual IL instructions in the debugger. It's different from some of the other round-trip IL inliners in that it (only) substitutes for entire function bodies, like this: class MyClass { [ILFunc(@" .locals init (

C# compiler doesn’t optimize unnecessary casts

拈花ヽ惹草 提交于 2019-12-02 20:15:37
A few days back, while writing an answer for this question here on overflow I got a bit surprised by the C# compiler, who wasn’t doing what I expected it to do. Look at the following to code snippets: First: object[] array = new object[1]; for (int i = 0; i < 100000; i++) { ICollection<object> col = (ICollection<object>)array; col.Contains(null); } Second: object[] array = new object[1]; for (int i = 0; i < 100000; i++) { ICollection<object> col = array; col.Contains(null); } The only difference in code between the two snippets is the cast to ICollection<object>. Because object[] implements