cil

Creating method dynamically, and executing it

北城余情 提交于 2019-11-30 10:41:19
问题 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

Writing a Compiler for .net - IL or Bytecode?

你说的曾经没有我的故事 提交于 2019-11-30 10:18:04
问题 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

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

烈酒焚心 提交于 2019-11-30 08:59:18
问题 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#? 回答1: 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

How is LINQ compiled into the CIL?

心不动则不痛 提交于 2019-11-30 08:43:41
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? 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 LINQ-to-Objects), then the lambda expression is turned into a separate method: private Car lambda0(Car c) {

MSIL debuggers - Mdbg, Dbgclr, Cordbg

拟墨画扇 提交于 2019-11-30 07:34:24
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? 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/2005/11/07/views_on_cordbg_and_mdbg.aspx Visual Studio is one you missed, but DbgClr should have the same

Does PowerShell compile scripts?

こ雲淡風輕ζ 提交于 2019-11-30 07:18:54
Suppose I have a simple PowerShell script: 1..3 | Write-Host How does PowerShell process it? Does it build in-memory assembly or some temporary .dll file? Can I examine this assembly and MSIL using some tools (e.g. ILSpy, VS, WinDbg)? Does PowerShell treat processing of file scripts and REPL command line input the same way (i.e. both compiled / interpreted)? Can I use this compiled assembly together with C# and other .Net languages? Can the PS script be compiled to a native binary code? Jason Shirk PowerShell V2 and earlier never compiled a script, it was always interpreted via virtual "eval"

How do i prevent my code from being stolen?

时间秒杀一切 提交于 2019-11-30 06:20:49
问题 What happens exactly when I launch a .NET exe? I know that C# is compiled to IL code and I think the generated exe file just a launcher that starts the runtime and passes the IL code to it. But how? And how complex process is it? IL code is embedded in the exe. I think it can be executed from the memory without writing it to the disk while ordinary exe's are not (ok, yes but it is very complicated). My final aim is extracting the IL code and write my own encrypted launcher to prevent

What is the (fnptr)* type and how to create it?

99封情书 提交于 2019-11-30 05:16:22
The following IL code creates a Type instance named (fnptr)* (token 0x2000000 - invalid, module mscorlib.dll). ldtoken method void* ()* call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle) What's the purpose of this type? Is it possible to create this type instance in C# without writing any IL code, maybe with reflection? Module.ResolveType on the token throws ArgumentOutOfRangeException . Edit: It's clear the (fnptr) type is an internal CLR type representation of an IL method pointer type, though when removing the last * , it

NOP in release build of F# code

坚强是说给别人听的谎言 提交于 2019-11-30 05:07:32
问题 I am playing with F# in VS2010 beta2, and since I am new to F#, I just picked one of the common examples and went ahead and implemented a factorial function as: let rec factorial n = if n <= 1 then 1 else n * factorial (n - 1);; If I build this and look at the generated code in Reflector, I get the corresponding C# code: public static int Factorial(int n) { if (n <= 1) return 1; return n * Factorial(n - 1); } So if I compile Reflector's C# representation of the F# code, I would expect to get

Compiler generated incorrect code for anonymous methods [MS BUG FIXED]

别来无恙 提交于 2019-11-30 04:22:05
See the following code: public abstract class Base { public virtual void Foo<T>() where T : class { Console.WriteLine("base"); } } public class Derived : Base { public override void Foo<T>() { Console.WriteLine("derived"); } public void Bang() { Action bang = new Action(delegate { base.Foo<string>(); }); bang(); //VerificationException is thrown } } new Derived().Bang(); throws an exception. Inside the generated CIL of the method Bang I got: call instance void ConsoleApp.Derived::'<>n__FabricatedMethod1'<string>() and the signature of the compiler generated method: method private hidebysig