cil

Creating a DynamicType in .NET implementing an interface but using member implementations from a base class

不打扰是莪最后的温柔 提交于 2019-11-30 01:42:24
问题 I am attempting to generate a dynamic class implementing an interface, but where one or more of the members already exists in the base. I compiled the following code in C# and examined it in reflector to see what the C# compiler does. class BaseClass { public string Bob { get { return "Bob"; } } } interface IStuff { string Bob { get; } } class SubClass : BaseClass, IStuff { } Reflector does not show any implementation in SubClass. .class private auto ansi beforefieldinit SubClass extends

DynamicMethod is much slower than compiled IL function

醉酒当歌 提交于 2019-11-29 22:41:20
I wrote a simple object copier that copies public properties. I can't figure out why the Dynamic method is a lot slower than the c# version. Durations C# method : 4,963 ms Dynamic method : 19,924 ms Note that - as I run the dynamic method before starting the stopwatch - the duration do not include the compilation phase. I run that in Debug and Release mode, in x86 and x64 mode, and from VS and from the command line with roughly the same result (dynamic method is 400% slower). const int NBRECORDS = 100 * 1000 * 1000; public class Person { private int mSomeNumber; public string FirstName { get;

Creating method dynamically, and executing it

懵懂的女人 提交于 2019-11-29 21:25:14
Background: I want to define few static methods in C# , and generate IL code as byte array, from one of these methods, selected at runtime (on client), and send the byte array over network to another machine (server) where it should be executed after re-generating the IL code from the byte array. My Attempt: ( POC ) public static class Experiment { public static int Multiply(int a, int b) { Console.WriteLine("Arguments ({0}, {1})", a, b); return a * b; } } And then I get the IL code of the method body, as: BindingFlags flags = BindingFlags.Public | BindingFlags.Static; MethodInfo meth = typeof

Writing a Compiler for .net - IL or Bytecode?

南笙酒味 提交于 2019-11-29 19:45:27
I'm currently diving into the inner workings of .net, which means IL. As an exercise, I want to build a brainf..k compiler for .net (yes, they already exist, but as said it's for learning purposes). For the moment I'm just writing some text files that contain .il and compile them with ilasm, which works. But I wonder if I could/should go one level deeper and write bytecode directly? My "concern" is the Windows PE Stuff when compiling an EXE - instead of ilasm I would need some sort of Bytecode linker that would take my MSIL/CIL bytecode and generate the PE Stuff for it? Or do compilers "only"

Behavior of F# “unmanaged” type constraint

狂风中的少年 提交于 2019-11-29 16:32:55
问题 F# supports a type constraint for "unmanaged". This is not the same as a value type constraint like "struct" constraints. MSDN notes that the behavior of the unmanaged constraint is: The provided type must be an unmanaged type. Unmanaged types are either certain primitive types (sbyte, byte, char, nativeint, unativeint, float32, float, int16, uint16, int32, uint32, int64, uint64, or decimal), enumeration types, nativeptr<_>, or a non-generic structure whose fields are all unmanaged types.

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

最后都变了- 提交于 2019-11-29 14:46:57
问题 I'm following up on my earlier question: Mono.Cecil: call base class' method from other assembly. I'm doing the same thing, but if my base class is generic it doesn't work. //in Assembly A class BaseVM<T> {} //in Assembly B class MyVM : Base<SomeModel> { [NotifyProperty] public string Something {get;set;} } It weaves the following code: L_000e: call instance void [AssemblyA]Base`1::RaisePropertyChanged(string) instead of L_000e: call instance void [AssemblyA]Base`1<class SomeModel>:

How is LINQ compiled into the CIL?

老子叫甜甜 提交于 2019-11-29 11:43:53
问题 For example: var query = from c in db.Cars select c; foreach(Car aCar in query) { Console.WriteLine(aCar.Name); } How would this translate once it is compiled? What happens behind the scenes? 回答1: It is compiled in the following way: First, the LINQ query expression is transformed into method calls: public static void Main() { var query = db.Cars.Select<Car, Car>(c => c); foreach (Car aCar in query) { Console.WriteLine(aCar.Name); } } If db.Cars is of type IEnumerable<Car> (which it is for

MSIL debuggers - Mdbg, Dbgclr, Cordbg

烂漫一生 提交于 2019-11-29 10:05:05
问题 I've been doing some MSIL work and have come across references to these three debuggers. What's the difference between them? Is one of them better than the others wrt. functionality? Are there others that I have missed? 回答1: I'm assuming you meant DbgClr not Clt and mdbg not mdbug? DbgClr uses the VS shell so you get a nice GUI. mdbg is the command line managed debugger. cordbg was an old sample that sorta shipped, but now it's just a wrapper for mdbg. http://blogs.msdn.com/jmstall/archive

Is there a way to hook a managed function in C# like I would a unmanaged function in C++?

末鹿安然 提交于 2019-11-29 09:33:39
In C++ I would get the address of the function and overwrite the first few bytes to a jmp to my function, do some stuff, restore the original bytes, and call the original function. Can I do something like this in C#? The .NET Profiler API is the closest "Microsoft-approved" way of intercepting methods at runtime. As already mentioned, this is somewhat difficult to implement and I am not aware of a library that makes this easy to implement using purely managed code. While researching options for this myself a few months ago, I stumbled on CLR Method Injection , which is an article with source

MS C# compiler and non-optimized code

不问归期 提交于 2019-11-29 09:17:06
Note: I noticed some errors in my posted example - editing to fix it The official C# compiler does some interesting things if you don't enable optimization. For example, a simple if statement: int x; // ... // if (x == 10) // do something becomes something like the following if optimized: ldloc.0 ldc.i4.s 10 ceq bne.un.s do_not_do_something // do something do_not_do_something: but if we disable optimization, it becomes something like this: ldloc.0 ldc.i4.s 10 ceq ldc.i4.0 ceq stloc.1 ldloc.1 brtrue.s do_not_do_something // do something do_not_do_something: I can't quite get my head around this