1,566 questions
4
votes
1
answer
129
views
`Common Language Runtime detected an invalid program` error generating simple dynamic method
I'm working on a JIT compiler for MIPS machine emulation to translate MIPS to CLI. I'm working on getting a nearly hard-coded Shift method, but whenever I try to execute the operation instruction on a ...
0
votes
0
answers
40
views
How do I use the "no." prefix instruction in CIL?
I found out about this instruction today and wanted to try it out. Sadly, ILASM doesn't seem to recognize it. That's why I decided to manually patch the generated assembly to add in the instruction. I ...
1
vote
0
answers
114
views
How to allow a custom default constructor for a struct?
I find myself occasionally annoyed by the empty constructor of C# struct types. Unlike classes, there's no way enforce that struct fields are initialized to non-default values, since all structs have ...
1
vote
1
answer
73
views
A lambda continuation with a generic type does not produce a stack overflow, but when I specify the type it results in a stack overflow. Why?
In a program I'm writing, I'm faced with a recursive type that I want to go through recursively (it seems necessary). Out of curiosity, I wanted to try and write a tail recursive version of my code, ...
4
votes
1
answer
127
views
Why does calling an unoverridden struct method require boxing?
According to boxing on structs when calling ToString(), calling an unoverridden method on a C# struct causes it to be boxed. This is ultimately because of how the constrained callvirt IL instruction ...
1
vote
1
answer
1k
views
Confused on C# MAUI - how does #if <platform> work?
I'm working with VS 17.11, and MAUI 8.
There are a lot of articles about "dependency injection", including on a platform basis.
But something like this seems possible too:
#if IOS
...
1
vote
1
answer
135
views
Why is there no extra memory allocation [duplicate]
When I was conducting the following benchmark test, the benchmark information indicated that there was no additional memory allocation. However, doesn't converting a struct to an interface involve ...
1
vote
1
answer
60
views
Mono.Cecil weaver wrong branch targets
I'm trying to use Mono.Cecil to weave in some instrumentation code into existing methods - basically just a log before all the sequence points. This is simple, I can iterate over all of the sequence ...
0
votes
0
answers
87
views
Is it possible to execute IL instructions one by one using System.Reflection.Emit?
In the preceding C# code, I'm creating a dynamic method and adding a few IL instructions to it, and then invoking the dynamic method:
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new(&...
-2
votes
1
answer
144
views
Is there an extra operation when using the negative number shorthand in C#?
In C# one can make a positive int negative using the minus sign - like so:
var positiveInt = 5;
var negativeInt = -positiveInt;
Is there an extra operation taking place under the hood when doing this ...
3
votes
1
answer
244
views
Why does 'return x switch' generates 'if (1 == 0)' in ILSpy?
I used ILSpy on my code from curiosity, and I noticed that if (1 == 0) empty instructions were added around return x switch statements.
Here is an example of this behavior:
public static string ...
0
votes
0
answers
110
views
How do I transform a function delegate to IL code
This question is a follow up on Unhandled exception: System.IO.FileNotFoundException: The file or assembly "System.Private.CoreLib" could not be found in custom generated assmbly.
I ...
1
vote
2
answers
97
views
sscanf returns 0 when run in CIL
I have this IL code from Expert .NET 2.0 IL Assembler by Serge Lidin:
This is in a file called CilTest.il:
//----------- Program header
.assembly extern mscorlib { auto }
.assembly OddOrEven { }
....
0
votes
0
answers
31
views
NullReferenceException on ldarg.1 instruction [duplicate]
We have a C#/.NET application running as a Windows Service. Infrequently, it logs several entries of the following form in the Windows Event Viewer.
Log Name: Application
Source: Windows Error ...
1
vote
0
answers
148
views
Dynamically redirect one C# method to another at runtime in .NET 8
I have been using the following method to redirect a C# method into another at runtime:
public class Injection
{
public static void install(MethodInfo methodToReplace, ...