cil

Generate tail call opcode

99封情书 提交于 2019-12-17 06:12:40
问题 Out of curiosity I was trying to generate a tail call opcode using C#. Fibinacci is an easy one, so my c# example looks like this: private static void Main(string[] args) { Console.WriteLine(Fib(int.MaxValue, 0)); } public static int Fib(int i, int acc) { if (i == 0) { return acc; } return Fib(i - 1, acc + i); } If I build it in release and run it without debugging I do not get a stack overflow. Debugging or running it without optimizations and I do get a stack overflow, implying that tail

Generate tail call opcode

爱⌒轻易说出口 提交于 2019-12-17 06:12:09
问题 Out of curiosity I was trying to generate a tail call opcode using C#. Fibinacci is an easy one, so my c# example looks like this: private static void Main(string[] args) { Console.WriteLine(Fib(int.MaxValue, 0)); } public static int Fib(int i, int acc) { if (i == 0) { return acc; } return Fib(i - 1, acc + i); } If I build it in release and run it without debugging I do not get a stack overflow. Debugging or running it without optimizations and I do get a stack overflow, implying that tail

How to get TypeDef from TypeSpec

怎甘沉沦 提交于 2019-12-14 03:58:03
问题 I'm trying to implement managed debugger looking at MDBG sample. Currently I'm stuck trying to get base class hierarchy methods using IMetaDataImport. EnumMethods, that I'm using, enumerates MethodDef tokens representing methods of the specified type . But I want to enumerate all the methods in class hierarchy. To do that I'm using GetTypeDefProps, that returns ptkExtends, which is token representing the base class. The problem is that base class could be represented by TypeDef, TypeRef or

OpCodes.Castclass. Is it necessary?

旧时模样 提交于 2019-12-13 15:03:07
问题 Is it necessary to emit OpCode.CastClass(typeof(A)) when you having a reference to instance of (B) on top of stack, where B is class, derived from A, when preparing for a call to method with argument of type A? Addition: interface IFoo { void IFoo(); } public class A:IFoo { public void IFoo() { } } public class B:A,IFoo { new public void IFoo() { } } var b = new B(); (b as IFoo).Foo(); ((b as A) as IFoo).Foo(); 回答1: I guess you have something like this: class A { public void Foo() { } } class

Intercept call to property get method in C#

我是研究僧i 提交于 2019-12-12 18:33:45
问题 Let's assume that we have this class: public class Person { public int Id { get; set; } public string Name { get; set; } } Now, is it possible in C# to intercept call to property get method, run some other method and return result of that method instead of property value? I'd like to be able to do some additional logic behind the scene. The downside is that this class can't be changed (on C# level). Maybe some IL ? 回答1: The .NET SDK has what you need already. You can round-trip most anything

Why does C# compiler emit additional OpCodes in IL?

孤街醉人 提交于 2019-12-12 10:48:19
问题 If I've a method Multiply defined as: public static class Experiment { public static int Multiply(int a, int b) { return a * b; } } Then why does the compiler emit this IL: .method public hidebysig static int32 Multiply(int32 a, int32 b) cil managed { .maxstack 2 //why is it not 16? .locals init ( [0] int32 CS$1$0000) //what is this? L_0000: nop //why this? L_0001: ldarg.0 L_0002: ldarg.1 L_0003: mul L_0004: stloc.0 //why this? L_0005: br.s L_0007 //why this? L_0007: ldloc.0 //why this? L

The curiosity of the let_ property method

杀马特。学长 韩版系。学妹 提交于 2019-12-12 09:32:00
问题 Every .net developer knows about the concept of properties. A rough 99.99%, it's just a piece of metadata gluing together two methods, a getter, and a setter. Same thing usually goes for events, with their add, remove, and invoke method. The ECMA-335 describes a «Other» kind of method semantic, that would apply to either a property or an event. Conceptually, a property or an event could have a multiple number of «other» methods. Today's the first day I stumbled upon a property with an «other»

ILDASM and ILASM, how use them?

橙三吉。 提交于 2019-12-12 09:08:16
问题 I'm having a hard time trying to (Dis)assemble a dll using the Visual Studio command prompt, don't look that simple as I see in the microsoft page. I'm using the ildasm for these way: ildasm myLibrary.dll output:MyLibrary.il And I get: Unable to open 'MyLibrary.il' for output. I also tried the full path: ildasm directory\myLibrary.dll output:directory\MyLibrary.il And I get: MULTIPLE INPUT FILES SPECIFIED Using the ildasm GUI actually works, but when I use the ilasm: ilasm myLibrary.il /dll I

If events are implemented as delegates in .NET, what is the point of the .event IL section?

 ̄綄美尐妖づ 提交于 2019-12-12 08:08:52
问题 I've seen some very good questions on Stack Overflow concerning delegates, events, and the .NET implementation of these two features. One question in particular, "How do C# Events work behind the scenes?", produced a great answer that explains some subtle points very well. The answer to the above question makes this point: When you declare a field-like event ... the compiler generates the methods and a private field (of the same type as the delegate). Within the class, when you refer to

Parse code with Regex - how to capture method's body with extra {… }? [duplicate]

这一生的挚爱 提交于 2019-12-12 06:43:45
问题 This question already has an answer here : Closed 7 years ago . Possible Duplicate: Parse CIL code with Regex This question comes from Parse CIL code with Regex To capture methods' body I added brackets () , it becomes var regex3 = @"(\.method\s[^{]+({(?!\s*}).*?}))"; and it worked fine. For example, capture.Groups[2] gives me { .entrypoint // .maxstack 8 IL_0000: nop IL_0001: call void TestAssemblyConsole.Test::Method1() IL_0006: nop IL_0007: call int32 TestAssemblyConsole.Test::Method2() IL