cil

What are the best resources for learning CIL (MSIL)

跟風遠走 提交于 2019-12-02 19:21:23
I'm an expert C# 3 / .NET 3.5 programmer looking to start doing some runtime codegen using System.Reflection.Emit.DynamicMethod. I'd love to move up to the next level by becoming intimately familiar with IL. Any pointers (pun intended)? The best way to learn it, is to write something you understand, then look at the IL it created. Also, depending on what you are doing, you can use expression trees instead of emitting IL, and then when you compile the expression trees, those smart guys at microsoft create the IL for ya. In addition to Darren's answer, I'd suggest picking or inventing a toy

Why are static classes considered “classes” and “reference types”?

浪子不回头ぞ 提交于 2019-12-02 18:08:15
I’ve been pondering about the C# and CIL type system today and I’ve started to wonder why static classes are considered classes. There are many ways in which they are not really classes: A “normal” class can contain non-static members, a static class can’t. In this respect, a class is more similar to a struct than it is to a static class, and yet structs have a separate name. You can have a reference to an instance of a “normal” class, but not a static class (despite it being considered a “reference type”). In this respect, a class is more similar to an interface than it is to a static class,

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

余生长醉 提交于 2019-12-02 16:39:59
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 new InvalidCastException("MyDouble value cannot convert to System.Double: no value present."); } } }

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

时间秒杀一切 提交于 2019-12-02 10:13:28
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? 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 files per class; or a single C# file (.cs) for the whole assembly. To decompile programmatically, use the

Is using static bytecode analysis to determine all the possible paths through a given method a variant of trying to solve the Halting Problem?

我的梦境 提交于 2019-12-02 01:27:41
问题 Is it possible to determine all the possible execution paths by reading the bytecode of a given method, or will that be equivalent to trying to solve the halting problem? If it can't be reduced to the halting problem, then how far can I go with static analysis without crossing the boundary of trying to solve the halting problem? Related question: "Finding all the code in a given binary is equivalent to the Halting problem." Really? 回答1: Yes, this is easily equivalent to solving the halting

Is using static bytecode analysis to determine all the possible paths through a given method a variant of trying to solve the Halting Problem?

廉价感情. 提交于 2019-12-01 22:42:41
Is it possible to determine all the possible execution paths by reading the bytecode of a given method, or will that be equivalent to trying to solve the halting problem? If it can't be reduced to the halting problem, then how far can I go with static analysis without crossing the boundary of trying to solve the halting problem? Related question: "Finding all the code in a given binary is equivalent to the Halting problem." Really? Yes, this is easily equivalent to solving the halting problem. Consider the following if statement: if (TuringMachine(x)) then goto fred; OK, is it really possible

What should I pin when working on arrays?

送分小仙女□ 提交于 2019-12-01 22:29:58
I'm trying to write a DynamicMethod to wrap the cpblk IL opcode . I need to copy chunks of byte arrays and on x64 platforms, this is supposedly the fastest way to do it. Array.Copy and Buffer.BlockCopy both work, but I'd like to explore all options. My goal is to copy managed memory from one byte array to a new managed byte array. My concern is how do I know how to correctly "pin" memory location. I don't want the garbage collector to move the arrays and break everything. SO far it works but I'm not sure how to test if this is GC safe. // copying 'count' bytes from offset 'index' in 'source'

Saving a DynamicMethod to disk

不问归期 提交于 2019-12-01 17:39:36
问题 I have inherited code that uses DynamicMethod to generate methods at runtime. I also need to modify some of the code that is being generated. Since I am a n00b at MSIL, I would love to be able to load the generated code up in Reflector and ensure that the code does what I pray that it does ;) Only, I can't figure out how to serialize the "Anonymously Hosted DynamicMethods Assembly" to disk. Is this possible? If so, how? 回答1: I think that if you want to load the method in Reflector or dotPeek,

What is the “<Module>” type?

我只是一个虾纸丫 提交于 2019-12-01 17:38:53
问题 I am using Mono.Cecil to read an assembly generated by Regex.CompileToAssembly() . When I iterate through the types, there is one type in the root namespace named <Module> . The type has no base type. What is this type? Is this some Mono.Cecil artifact or something that is actually a real part of .NET assemblies? What role does it play? 回答1: The <Module> type is a place-holder for declarations that do not fit the CLI model. Normally relevant only in assemblies that are mixed-mode, containing

Do method names get compiled into the EXE?

余生颓废 提交于 2019-12-01 17:04:32
Do class, method and variable names get included in the MSIL after compiling a Windows App project into an EXE? For obfuscation - less names, harder to reverse engineer. And for performance - shorter names, faster access. e.g. So if methods ARE called via name: Keep names short , better performance for named-lookup. Keep names cryptic , harder to decompile. Yes, they're in the IL - fire up Reflector and you'll see them. If they didn't end up in the IL, you couldn't build against them as libraries. (And yes, you can reference .exe files as if they were class libraries.) However, this is all