cil

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

Member is declared in another module and needs to be imported

江枫思渺然 提交于 2019-12-10 13:27:38
问题 I use Mono.Cecil to create a new custom attribute type and then add it to an existing type. To demonstrate it, I have a pre-existing DLL called "Sample" with type that is called "SampleType". I want to use Mono.Cecil to weave in a new type in "Sample" called "NewAttribute" and then add this attribute to "SampleType". The code looks like this: (not exactly but its good enough for example) static void AddCustomeAttribute() { var module = ModuleDefinition.ReadModule(AssemblyName); var attrType =

Array of pointers in C++/CLI MSIL assembly

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 03:34:28
问题 I'm trying to wrap some legacy C code for use with C# running on .NET Core. I'm using the approach given here to create a C++ wrapper that compiles to pure MSIL. It's working well for simple functions, but I've found that if my code ever uses pointers-to-pointers or arrays of pointers it will crash with a memory violation. Often it crashes Visual Studio and I have to restart everything, which is tedious. For example, the following code will cause the crashes: public ref class example { public

Why does tail call optimization need an op code?

萝らか妹 提交于 2019-12-10 01:06:44
问题 So I've read many times before that technically .NET does support tail call optimization (TCO) because it has the opcode for it, and just C# doesn't generate it. I'm not exactly sure why TCO needs an opcode or what it would do. As far as I know, the requirement for being able to do TCO is that the results of a recursive call are not combined with any variables in the current function scope. If you don't have that, then I don't see how an opcode prevents you from having to keep a stack frame

How to mutate a boxed value type (primitive or struct) in C#/IL

末鹿安然 提交于 2019-12-08 13:07:14
问题 Related to How to mutate a boxed struct using IL I am trying to change the value of a boxed value type but in a generic way, so trying to implement the following method: void MutateValueType<T>(object o, T v) where T : struct So the following should be possible: var oi = (object)17; MutateValueType<int>(oi, 43); Console.WriteLine(oi); // 43 var od = (object)17.7d; MutateValueType<double>(od, 42.3); Console.WriteLine(od); // 42.3 I am failing to get it this to work on .NET Framework (see

How to declare a value type in CIL: `.class value` or just `.class`?

 ̄綄美尐妖づ 提交于 2019-12-07 07:49:41
问题 I have taken a look at a C# struct FooStruct in ILDASM, and have seen the following: ILDASM here displays two differing declarations: one starting with .class value public (rear window & front window's title bar) one starting with just .class public (front window) And I wonder which syntax (if not both) is the correct one for declaring a value type? Is the value modifier strictly necessary, or optional, or a syntax error? 回答1: Short answer: Value type definitions only require extends

IDE for MSIL

喜欢而已 提交于 2019-12-07 04:18:39
问题 There's any number of questions about MSIL on SO but none that directly answer this. I've been using Textpad which does have a syntax definition file for MSIL and then using ILASM in a command prompt window. I did find a reference to ILIDE but the link is broken. Is there an IDE or add-on / plugin to Visual Studio / Eclipse for MSIL that allows syntax highlighting, intellisense, code completion, debugging etc.? 回答1: Use MonoDevelop You can build specially MSIL projects. It's very useful for

.NET CIL manipulation of evaluation stack

假如想象 提交于 2019-12-07 04:07:10
问题 I have this sequence of CIL codes which I injected through the use of Mono.Cecil . However, the modified .NET C# application will not run. Objective: Manually load and pop values from stack to display in Console.WriteLine for (int i = 0; i < 3; i++) { int z = some value popped manually from stack; Console.WriteLine(z); } This is the simple main() program I modified: .method private hidebysig static void Main(string[] args) cil managed { .entrypoint .maxstack 5 .locals init ( [0] int32 num, [1

Why does the IL set this value twice?

爷,独闯天下 提交于 2019-12-07 01:24:41
问题 I was trying around a bit with Try Roslyn when I entered this piece of code: using System; using System.Linq; using System.Collections.Generic; using Microsoft.CSharp; public class C { public C() { x = 4; } public int x { get; } = 5; } And it gave me back this code: using System; using System.Diagnostics; using System.Reflection; using System.Runtime.CompilerServices; using System.Security; using System.Security.Permissions; [assembly: AssemblyVersion("0.0.0.0")] [assembly: Debuggable

Is C# faster than VB.NET? [closed]

早过忘川 提交于 2019-12-06 18:42:51
问题 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. Closed 9 years ago . You'd think both are the same. But maybe it's the compiler that Microsoft has used, but I've noticed that when compiling two very