cil

Does C# System.String Instances Really End Up on the Heap?

寵の児 提交于 2019-12-03 13:08:23
问题 Let's consider some very simple C# code: static void Main(string[] args) { int i = 5; string s = "ABC"; bool b = false; } Jeffrey Richter's " CLR via C# " (Chapter 14) states that " The String type is derived immediately from Object, making it a reference type, and therefore, String objects (its array of characters) always live in the heap, never on a thread's stack ". Also referring to strings, on an example in the book quite similar to the one above: " The newobj IL instruction constructs a

CIL stack exchange instruction

陌路散爱 提交于 2019-12-03 12:58:26
Is there a CIL instruction to exchange the first two elements in the stack? There is no single instruction exchange. However, using stloc , pop , and ldloc , you should be able to accomplish your exchange. No. The only way to swap elements is to pop the top two elements to locals, then push them in reverse order. Looking at a list of CIL instructions there doesn't appear to be a single instruction that exchanges the two elements at the top of the stack. You'll have to do it the old pop/push way. For future reference, you can create an assembly that does what you want to learn the IL for, then

Fast serialization and deserialization using dynamically emitted POCOs

≡放荡痞女 提交于 2019-12-03 11:17:25
问题 I am currently serializing SQL table rows into a binary format for efficient storage. I serialize/deserialize the binary data into a List<object> per row. I'm trying to upgrade this to use POCOs, that will be dynamically generated (emitted) with one Field per column. I've been searching online for hours and have stumbled upon ORMs/frameworks like EF, T4, ExpandoObject, but all of these either use a dynamic object (properties can be added/removed on the fly) or simply generate a POCO before

Can C/C++ software be compiled into bytecode for later execution? (Architecture independent unix software.)

心已入冬 提交于 2019-12-03 09:59:03
问题 I would want to compile existing software into presentation that can later be run on different architectures (and OS). For that I need a (byte)code that can be easily run/emulated on another arch/OS (LLVM IR? Some RISC assemby?) Some random ideas: Compiling into JVM bytecode and running with java. Too restricting? C-compilers available? MS CIL. C-Compilers available? LLVM? Can Intermediate representation be run later? Compiling into RISC arch such as MMIX. What about system calls? Then there

Why is it so easy to decompile .NET IL code?

别等时光非礼了梦想. 提交于 2019-12-03 08:54:03
问题 Why is it so easy to decompile .NET IL-code into source code, compared to decompiling native x86 binaries? (Reflector produces quite good source code most of the time, while decompiling the output of a C++ compiler is almost impossible.) Is it because IL contains a lot of meta data? Or is it because IL is a higher abstraction than x86 instructions? I did some research and found the following two usefull articles, but neither of them answers my question. MSIL Decompiler Theory C Decompiler -

Why is it necessary to call :this() on a struct to use automatic properties in c#?

百般思念 提交于 2019-12-03 08:04:29
问题 If I define a struct in C# using automatic properties like this: public struct Address { public Address(string line1, string line2, string city, string state, string zip) { Line1 = line1; Line2 = line2; City = city; State = state; Zip = zip; } public string Line1 { get; protected set; } public string Line2 { get; protected set; } public string City { get; protected set; } public string State { get; protected set; } public string Zip { get; protected set; } } When I attempt to build the file,

Is there a good wrapper around ILGenerator? [closed]

天大地大妈咪最大 提交于 2019-12-03 07:11:08
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. I'm using System.Reflection.Emit for a while now, and find it (who don't?) as painful as bug prone. Do you know if there is a good wrapper around the IL Generator, something that I can rely on to emit IL in a more safe and easier manner than with playing

How to insert CIL code to C#

别来无恙 提交于 2019-12-03 06:57:25
问题 Is it possible to insert IL code to C# method? 回答1: I just posted a utility which allows entire C# function bodies to be automatically replaced with inline IL, using a custom attribute. Like similar utilities, this works via the ILDASM/ILASM round-trip which can be set up as a post-build step. The tool also adjusts the PDB in order to preserve single-stepping and setting breakpoints on individual IL instructions in the debugger. It's different from some of the other round-trip IL inliners in

C# compiler doesn’t optimize unnecessary casts

最后都变了- 提交于 2019-12-03 06:36:53
问题 A few days back, while writing an answer for this question here on overflow I got a bit surprised by the C# compiler, who wasn’t doing what I expected it to do. Look at the following to code snippets: First: object[] array = new object[1]; for (int i = 0; i < 100000; i++) { ICollection<object> col = (ICollection<object>)array; col.Contains(null); } Second: object[] array = new object[1]; for (int i = 0; i < 100000; i++) { ICollection<object> col = array; col.Contains(null); } The only

Why does 'unbox.any' not provide a helpful exception text the way 'castclass' does?

不羁岁月 提交于 2019-12-03 04:34:26
To illustrate my question, consider these trivial examples (C#): object reference = new StringBuilder(); object box = 42; object unset = null; // CASE ONE: bad reference conversions (CIL instrcution 0x74 'castclass') try { string s = (string)reference; } catch (InvalidCastException ice) { Console.WriteLine(ice.Message); // Unable to cast object of type 'System.Text.StringBuilder' to type 'System.String'. } try { string s = (string)box; } catch (InvalidCastException ice) { Console.WriteLine(ice.Message); // Unable to cast object of type 'System.Int32' to type 'System.String'. } // CASE TWO: bad