il

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

Does ldstr internally implement newobj?

▼魔方 西西 提交于 2019-12-10 13:28:59
问题 As we all know strings are implicitly instantiated, meaning that we don't have to use new in order to get a reference to an object of one. Because of this it was always my belief that the framework is taking care of this, and hence I would get identical IL if I did something like this: String first = new String(new char[] {'a'}); string second = "a"; However it appears that the first line is done using newobj instance void [mscorlib]System.String::.ctor(char[]) and the second ldstr "a" . So

Why is it not possible to get local variable names using Reflection?

徘徊边缘 提交于 2019-12-09 15:14:09
问题 If I have a code like this: public class Program { public static void Main() { string bar = ""; int foo = 24; } } I can get the local variables declared in Main using: var flag = BindingFlags.Static | BindingFlags.Public; var fields = typeof(Program).GetMethod("Main", flags).GetMethodBody().LocalVariables; This returns a IList<LocalVariableInfo> and the LocalVariableInfo has only three properties: IsPinned , LocalIndex and LocalType .So there is no Name property exists. What I'm wondering is

Replace references to a type/namespace using Mono.Cecil

梦想与她 提交于 2019-12-09 12:31:27
问题 Background (unnecessary, confusing, only for the curious) I'm using the free version of Unity3D for Mobile and it doesn't allow me to use the System.Net.Sockets namespace on mobile devices. The problem is that I'm using a compiled .dll library (namely, IKVM) that references the System.Net.Sockets . I'm not actually using the classes in IKVM that references that references System.Net.Sockets , so instead of buying the $3000 Unity Pro mobile licenses, I made a stub library of the Sockets

Purpose and Meaning of “specialname” and “rtspecialname” in IL

只谈情不闲聊 提交于 2019-12-08 20:45:34
问题 I am trying to understand the IL code and C# internals specifically nowadays, i wrote a simple c# hello world program whose code is : using System; class Program { public static void Main() { Console.WriteLine("Hello World"); } } and here is IL generated for the constuctor of Program class : .method public hidebysig specialname rtspecialname instance void .ctor() cil managed { // Code size 7 (0x7) .maxstack 8 IL_0000: ldarg.0 IL_0001: call instance void [mscorlib]System.Object::.ctor() IL

Why does C# compiler produce method call to call BaseClass method in IL

喜你入骨 提交于 2019-12-08 15:13:28
问题 Lets say we have following sample code in C#: class BaseClass { public virtual void HelloWorld() { Console.WriteLine("Hello Tarik"); } } class DerivedClass : BaseClass { public override void HelloWorld() { base.HelloWorld(); } } class Program { static void Main(string[] args) { DerivedClass derived = new DerivedClass(); derived.HelloWorld(); } } When I ildasmed the following code: .method private hidebysig static void Main(string[] args) cil managed { .entrypoint // Code size 15 (0xf)

How to extract class IL code from loaded assembly and save to disk?

三世轮回 提交于 2019-12-08 13:33:26
问题 How would I go about extracting the IL code for classes that are generated at runtime by reflection so I can save it to disk? If at all possible. I don't have control of the piece of code that generates these classes. Eventually, I would like to load this IL code from disk into another assembly. I know I could serialise/deserialise classes but I wish to use purely IL code. I'm not fussed with the security implications. Running Mono 2.10.1 回答1: Or better yet, use Mono.Cecil. It will allow you

Is it possible to inspect an assembly's IL instructions programmatically using managed code?

落爺英雄遲暮 提交于 2019-12-08 04:18:42
问题 See title. Reflection.Emit seems to be more about creating a new dynamic assembly, not for loading an exisitng assembly and inspecting its IL. 回答1: Common Compiler Infrastructure 回答2: Reflector does this, and last time I checked, Reflector could still inspect (i.e. disassemble) itself this way, so it will show you exactly how it works. 来源: https://stackoverflow.com/questions/2824086/is-it-possible-to-inspect-an-assemblys-il-instructions-programmatically-using-m

How to understand the class name add <> symbol?

£可爱£侵袭症+ 提交于 2019-12-08 01:54:42
问题 How to understand this code in C#? using System; internal class <Module> { } The class name can't contain the symbol <> but it is allowed to have it in code? How would you achieve this? And how can I understand this difference? Take a look at this code: internal class <Module>{D8BAC701-C957-4CBE-8F97-37BB3A7DAFFE} { static <Module>{D8BAC701-C957-4CBE-8F97-37BB3A7DAFFE}() { if (!(eqXmHMg8VVPy3nYeAo.eHiMp4DL8(Convert.ToBase64String(Type.GetTypeFromHandle(epOl6XynGueNUNwY5Y

Questions about hand coded IL based on disassembled simple C# code

时光怂恿深爱的人放手 提交于 2019-12-07 02:07:29
问题 I just started looking at IL a bit and I'm curious if my attempt (shown below) to remove excess code from the output of the compiler had any unintended side effects. A couple of quesiton about the results: What is the purpose of the nop operations in the original? What is the purpose of the br.s at the end of the methods in the original? Is the re-written version improper in any way? Original C# Code: class Program { public static int Main() { return Add(1, 2); } public static int Add(int a,