cil

Managed .NET debugger and async/await methods

╄→гoц情女王★ 提交于 2020-01-24 13:20:38
问题 I'm making a managed .NET debugger using MDBG sample. Consider some simple async example: 1: private async void OnClick(EventArgs args){ 2: var obj = new SomeClass(); 3: bool res = await StaticHelper.DoSomeAsyncStuff(); 4: if(res){ 5: Debug.WriteLine("result is True"); 6: } 7: else{ 8: Debug.WriteLine("result is False"); 9: } 10: someField = obj.Name + "is:" + res.ToString(); 11: } 12: public static async Task<bool> DoSomeAsyncStuff(){ 13: await Task.Delay(5000); 14: return true; 15: }

How to use .net cil jmp opcode

旧街凉风 提交于 2020-01-24 10:47:06
问题 I'm trying to get the jmp opcode to work in Cil jmp void ILTest.Program::MyFunc2(int32) ilasm is fine with it, but when I run the program I always get "common language runtime detects an invalid program" exception. I know this is unverifiable code so I have tried to give permissions SecurityPermission perm = new SecurityPermission(SecurityPermissionFlag.Execution | SecurityPermissionFlag.SkipVerification | SecurityPermissionFlag.UnmanagedCode); but it does not seem to have any effect. Has

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

╄→尐↘猪︶ㄣ 提交于 2020-01-22 17:36:06
问题 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

Check if type TA can cast to type TB at runtime? (Considering something more than inheritance)

孤街醉人 提交于 2020-01-13 11:30:09
问题 So I know that this works: class A { } class B : A { } [Test] public void CanCast() { Assert.That(typeof(A).IsAssignableFrom(typeof(B))); Assert.That(!typeof(B).IsAssignableFrom(typeof(A))); } However, let's say those two types were Int32 and Int64. At runtime, I can cast an Int32 value to an Int64 variable, but not the other way around. How can I check this kind of casting-compatibility at runtime? (IsAssignableFrom doesn't work for this, it always gives false for Int32 and Int64) EDIT: I

Performance penalty when Generic.List<T>.Add is the the last statement in a function and tailcall optimization is on

你说的曾经没有我的故事 提交于 2020-01-13 08:49:18
问题 I've run into a strange performance penalty that I've boiled down to this code: [<Struct>] type Vector3(x: float32, y: float32, z: float32) = member this.X = x member this.Y = y member this.Z = z type Data(n: int) = let positions = System.Collections.Generic.List<Vector3>() let add j = positions.Add (Vector3(j, j, j)) let add1 j = positions.Add (Vector3(j, j, j)); () member this.UseAdd () = for i = 1 to n do add (float32 i) member this.UseAdd1 () = for i = 1 to n do add1 (float32 i) let

Using assemblies compiled from IL with .NET Core & Xamarin

主宰稳场 提交于 2020-01-12 05:34:45
问题 Updated with a solution that works for me. See the bottom of this question. Context: I needed a way to evaluate the size of a generic type for the purpose of calculating array lengths to fit within a certain byte size. Basically, sizeof similar to what C/C++ provides. C#'s sizeof and Marshal.SizeOf are not suitable for this, because of their many limitations. With this in mind, I wrote an assembly in IL that enables the functionality I was looking for through the sizeof opcode. I'm aware that

Is there a simple way to obtain all the local variables in the current stack frame in C# (or CIL)

落爺英雄遲暮 提交于 2020-01-09 21:17:43
问题 Following my previous question, in which I wanted to dump all the variables in the stack (from the current and all the previous frame) that can be seen here: Is there a way to examine the stack variables at runtime in C#? I was suggested to intercept the calls manually or use AOP framework like PostSharp to simplify such a task. I looked at PostSharp and the interception arguments don't include the variables in the current stack frame. I wonder if there is a simple way to automatically get

How to validate C# code?

若如初见. 提交于 2020-01-05 07:26:07
问题 I'm working on a WPF application that validates C# code from files. I was able to get the file and, for a different need, instantiate its type. Now what I need is validate that code against some criteria I set. What do I mean? Let's say I have a file "Test.cs" and this file has the following code: using xpto; using abcd; public class Test { public static void Testing() { Iqueryable<XYZ> var1 = ctx.Where(c => c.IdSomething == number); var1 = var1.Where(v => v.Count(x => x.ValZ) > 0); } } In my

Compiler evaluation of explicit null-check vs. null-coalescing operator?

五迷三道 提交于 2020-01-03 16:24:51
问题 Consider the following code, which uses two slightly different methods to check _instance and assign it when not already set. class InstantiationTest { private Object _instance; public void Method1() { if(_instance == null) { _instance = new Object(); } } public void Method2() { _instance = _instance ?? new Object(); } } Either VS or Resharper keeps underlining my explicit null checks, and prompting me to refactor using the null-coalescing operator. I wondered whether the compiler is smart

“Operation could destablize the runtime” and DynamicMethod with value types

北战南征 提交于 2020-01-03 09:54:51
问题 I'm trying to generalize the following IL (from Reflector): .method private hidebysig instance void SetValue(valuetype Test.TestFixture/ValueSource& thing, string 'value') cil managed { .maxstack 8 L_0000: nop L_0001: ldarg.1 L_0002: ldarg.2 L_0003: call instance void Test.TestFixture/ValueSource::set_Value(string) L_0008: nop L_0009: ret } However, when I try and reproduce this IL with DynamicMethod: [Test] public void Test_with_DynamicMethod() { var sourceType = typeof(ValueSource);