cil

How to Emit code to assign value/reference to static field of class by calling it's constructor?

社会主义新天地 提交于 2019-12-11 20:19:43
问题 (My code is somewhat a mess of C# and VB.NET) I am trying to Emit class that looks as following: public class SWTTFields { private string fieldName; private int startPosition; private int endPosition; public static readonly SWTTFields ISO = new SWTTFields("ISO", 1, 2); public static readonly SWTTFields EPC = new SWTTFields("EPC", 3, 4); private SWTTFields(String fieldName, Int32 startPositon, Int32 endPositon) { this.fieldName = fieldName; this.startPosition = startPositon; this.endPosition =

IL method to pin a ref T value as void* (to work on Span<T> from parallel code)

随声附和 提交于 2019-12-11 15:57:20
问题 Before the actual question, a small disclaimer: This is a related/follow up question to this one, but since here I'm talking about a more general issue (how to pin a ref T variable to be able to perform pointer operations on it), and I'm proposing a possible (probably wrong) solution, I opened a separate question. So, the question is: given a ref T variable (assuming it's the first item of an array), how can I pin it so that the GC won't cause problems while working on the underlying array

Redirecting to a dynamic method from a generic event handler

末鹿安然 提交于 2019-12-11 05:29:15
问题 I'm trying to write a class that's to be used to trigger a call to a method from an arbitrary event but I'm stuck as I simply cannot figure out a way to reference 'this' from emitted MSIL code. This example should describe what I'm looking for: class MyEventTriggeringClass { private object _parameter; public void Attach(object source, string eventName, object parameter) { _parameter = parameter; var e = source.GetType().GetEvent(eventName); if (e == null) return; hookupDelegate(source, e); }

Is overriding a final (IL) / sealed (C#) method with a different name legal?

被刻印的时光 ゝ 提交于 2019-12-11 00:00:00
问题 I have a hierarchy of classes: class C1 { virtual object M1(); } class C2: C1 { override sealed object M1(); } class C3: C2 { // I want to override M1() // CSC gives me an error, obviously override object M1(); } But it seems there is a way. In IL, you can override a method with a different name. So, we change the name ( M1_2() overrides M1() ), say it overrides the method on the base class ( C1::M1() ), a la explicit interface implementation, and the "final" on the intermediate ( C2 ) class

Mono.Cecil: call base class' method from other assembly

泄露秘密 提交于 2019-12-10 21:49:28
问题 How can I get a MethodReference to a base class' method by name? I've tried type.BaseType.Resolve().Methods; and if I add the dll containing the base class to the assemblyresolver it returns the methods. But if I add a call using MSILWorker.Create(OpCodes.Call, baseMethod); (where baseMethod was found by foreaching Methods from the resolved TypeDefinition) the resulting IL is unreadable, even Reflector freezes and quits. Now some IL: if calling private method on type: call instance void

IL constrained call

北城以北 提交于 2019-12-10 16:16:02
问题 For this code: class Program { static void Main() { Console.WriteLine(new MyStruct().ToString()); } struct MyStruct { } } the C# compiler generates constrained callvirt IL code. This article says: For example, if a value type V overrides the Object.ToString() method, a call V.ToString() instruction is emitted; if it does not, a box instruction and a callvirt Object.ToString() instruction are emitted. A versioning problem can arise <...> if an override is later added. So, my question is: why

MSIL : Superfluous branch

一笑奈何 提交于 2019-12-10 14:46:32
问题 Consider this C# snippet: static string input = null; static string output = null; static void Main(string[] args) { input = "input"; output = CallMe(input); } public static string CallMe(string input) { output = "output"; return output; } Dissassembling using Reflector shows: .method private hidebysig static void Main(string[] args) cil managed { .entrypoint .maxstack 8 L_0000: nop L_0001: ldstr "input" L_0006: stsfld string Reflector_Test.Program::input L_000b: ldsfld string Reflector_Test

CIL unbox_any instruction - strange behavior

為{幸葍}努か 提交于 2019-12-10 14:06:35
问题 .method public static void Test<class T>(object A_0) cil managed { // Code size 13 (0xd) .maxstack 1 .locals init (!!T V_0) IL_0000: ldarg.0 IL_0001: isinst !!T IL_0006: unbox.any !!T IL_000b: stloc.0 IL_000c: ret } // end of method DemoType::Test The equal C# code is: public static void Test<T>(object o) where T : class { T t = o as T; } My questions are: Why unbox.any been called? if you just do var a = father as child isinst intruction will call and no unbox.any, and If i'll remove the

How does F# compile functions that can take multiple different parameter types into IL?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 13:59:24
问题 I know virtually nothing about F#. I don’t even know the syntax, so I can’t give examples. It was mentioned in a comment thread that F# can declare functions that can take parameters of multiple possible types, for example a string or an integer. This would be similar to method overloads in C#: public void Method(string str) { /* ... */ } public void Method(int integer) { /* ... */ } However, in CIL you cannot declare a delegate of this form. Each delegate must have a single, specific list of

How Do VB.NET Optional Parameters work 'Under the hood'? Are they CLS-Compliant?

依然范特西╮ 提交于 2019-12-10 13:46:48
问题 Let's say we have the following method declaration: Public Function MyMethod(ByVal param1 As Integer, _ Optional ByVal param2 As Integer = 0, _ Optional ByVal param3 As Integer = 1) As Integer Return param1 + param2 + param3 End Function How does VB.NET make the optional parameters work within the confines of the CLR? Are optional parameters CLS-Compliant? 回答1: Interestingly, this is the decompiled C# code, obtained via reflector. public int MyMethod(int param1, [Optional,