cil

Is there a free tool capable of pruning unused code from a CLI assembly? [closed]

醉酒当歌 提交于 2019-12-04 06:42:31
Closed. This question is off-topic. It is not currently accepting answers. Learn more . Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . Is there a free tool capable of pruning unused code from a CLI assembly? I know there are obfuscators that are capable of performing this optimization, but these all cost money. Is there a free (or even open source) tool that removes the unused code in an already compiled assembly? Jb Evain There is. It's called the Mono.Linker . What I wrote three years about the Mono.Linker ago pretty much still

In IL code, why is there not a nop opcode in a given situation? Why is there a br.s opcode in a given situation?

不羁岁月 提交于 2019-12-04 05:07:57
Suppose I have the following code: public class Class1 { private Class2 obj; public void MethodA() { var class2 = new Class2(); class2.PropertyI = 2; obj = MethodB(class2); } public Class2 MethodB(Class2 class2) { return class2; } } public class Class2 { public int PropertyI { get; set; } } The generated IL code from compiling with Visual Studio 2010 as a .NET 2.0 assembly is the following: .method public hidebysig instance void MethodA() cil managed { .maxstack 3 .locals init ( [0] class ClassLibrary1.Class2 class2) L_0000: nop L_0001: newobj instance void ClassLibrary1.Class2::.ctor() L_0006

What is the difference between Expression bodied syntax vs Getter syntax on IL level?

末鹿安然 提交于 2019-12-04 04:27:43
问题 I would like to understand the IL code a bit. So as you see the expression bodied has less code than the GetOld code. Is it some optimization done there and means expression body syntax is more performant? Or it does not really matter? namespace DatabaseModules { public class Test { public IList<string> _cache = new List<string>(); public Test() { } public IList<string> Get => _cache; public IList<string> GetOld { get { return _cache; } } } } And the generated IL Code using DotPeek https:/

Debugging in .NET in Release mode

落花浮王杯 提交于 2019-12-04 03:23:08
问题 Some time ago I've read an article on CLR, where author showed that if a project is compiled in DEBUG mode, before each operator comes a NOP command, thus allowing to debug a code. Nevertheless, today I discovered that we can also debug in release mode as well... Please help to understand the difference. 回答1: You can debug in Release mode to an extent. Debug and Release are simply build configurations (of which you can create many), the real difference is that Debug configuration doesn't

Emit local variable and assign a value to it

醉酒当歌 提交于 2019-12-04 02:37:49
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; 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(OpCodes.Ldc_I4, 6); // Store "6" ... ilGen.Emit(OpCodes.Stloc, b); // ... in "b". ilGen.Emit(OpCodes.Ldloc,

Why Local Functions generate IL different from Anonymous Methods and Lambda Expressions?

夙愿已清 提交于 2019-12-04 00:45:25
Why the C# 7 Compiler turns Local Functions into methods within the same class where their parent function is. While for Anonymous Methods (and Lambda Expressions) the compiler generates a nested class for each parent function, that will contain all of its Anonymous Methods as instance methods ? For example, C# code (Anonymous Method) : internal class AnonymousMethod_Example { public void MyFunc(string[] args) { var x = 5; Action act = delegate () { Console.WriteLine(x); }; act(); } } Will produce IL Code (Anonymous Method) similar to: .class private auto ansi beforefieldinit AnonymousMethod

Where can I find a list of escaped characters in MSIL string constants?

坚强是说给别人听的谎言 提交于 2019-12-03 22:35:35
I've written a program (in C#) that reads and manipulates MSIL programs that have been generated from C# programs. I had mistakenly assumed that the syntax rules for MSIL string constants are the same as for C#, but then I ran into the following situation: This C# statement string s = "Do you wish to send anyway?"; gets compiled into (among other MSIL statements) this IL_0128: ldstr "Do you wish to send anyway\?" I wasn't expecting the backslash that is used to escape the question mark. Now I can obviously take this backslash into account as part of my processing, but mostly out of curiosity I

What's the different between ldsfld and ldstr in IL?

三世轮回 提交于 2019-12-03 20:54:05
问题 I've read some article about String.Empty vs "" and I also do test by my self. Different between them are below. String.Empty L_0001: ldsfld string [mscorlib]System.String::Empty "" L_0001: ldstr "" After I talk with my friends they argue that String.Empty is faster than "" because under the hood (at assembly level) ldstr do more 1 circle than ldsfld. (I can't remember steps that make them different) I want to know how can I check about this aspect of performance. 回答1: The ldsfld operation

How to convert CIL to a LINQ Expression Tree [closed]

廉价感情. 提交于 2019-12-03 16:32:33
Has any work been done on the direct conversion of CIL to LINQ expression trees? This would include class libraries, blogs, books, academic papers, etc. What are the known mismatches between CIL and LINQ Expression API? I have seen a project that significantly extended the Expression API to support an almost complete mapping between C# and LINQ Expressions. What work arounds would be suggested to accommodate any of these mismatches? Is there something fundamentally wrong about converting directly from CIL to LINQ Expressions? If so, why? 来源: https://stackoverflow.com/questions/12824201/how-to

Linking a .NET Expression Tree into a new assembly

故事扮演 提交于 2019-12-03 15:38:22
I'm trying to write my own toy My Toy Language -> MSIL compiler in order to get a better understanding of how compilers work. I got the parsing and lexing working, I have built the expression trees and using the System.Linq.Expressions expression tree API, I have a working interpreter. Now I would like to emit some real MSIL assemblies. The problem is, I can't figure out how to actually build these assemblies. The MethodBuilder class only accepts raw MSIL method bodies, so I have to get the raw MSIL of my expression tree. Calling Expression.Compile() returns a working delegate but I'm not able