C
C#8mo ago
Finnland

❔ how do i print the bytecode generated from a ILGenerator? (im getting an error)

i am trying to generate and print csharp bytecode using ILGenerator and AssemblyBuilder, ModeuleBuilder, etc, but when i try to get my bytecode using the code below, i get the following error:
'System.InvalidOperationException' occurred in System.Private.CoreLib.dll: 'Operation is not valid due to the current state of the object.'
'System.InvalidOperationException' occurred in System.Private.CoreLib.dll: 'Operation is not valid due to the current state of the object.'
my code:
AssemblyName assemblyName = new AssemblyName("CSharpBytecode");
AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);

ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("CSharpBytecode");
TypeBuilder typeBuilder = moduleBuilder.DefineType("CSharpBytecode", TypeAttributes.Public);
MethodBuilder methodBuilder = typeBuilder.DefineMethod("MyMethod", MethodAttributes.Public);

ILGenerator ilGenerator = methodBuilder.GetILGenerator();

ilGenerator.Emit(OpCodes.Ldstr, "Hello, world!");
ilGenerator.Emit(OpCodes.Ret);

byte[] methodBody = methodBuilder.GetMethodBody().GetILAsByteArray(); // this errors
AssemblyName assemblyName = new AssemblyName("CSharpBytecode");
AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);

ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("CSharpBytecode");
TypeBuilder typeBuilder = moduleBuilder.DefineType("CSharpBytecode", TypeAttributes.Public);
MethodBuilder methodBuilder = typeBuilder.DefineMethod("MyMethod", MethodAttributes.Public);

ILGenerator ilGenerator = methodBuilder.GetILGenerator();

ilGenerator.Emit(OpCodes.Ldstr, "Hello, world!");
ilGenerator.Emit(OpCodes.Ret);

byte[] methodBody = methodBuilder.GetMethodBody().GetILAsByteArray(); // this errors
PS, i have no clue if this is beginner, intermediate, or advanced (and im likely in way over my head here)
13 Replies
exokem
exokem8mo ago
Is the error from GetMethodBody or GetILAsByteArray?
Finnland
Finnland8mo ago
getMethodBody i tried looking it up and it might be that the method doesnt have a body, but the only way i can find to give it a body is exactly what im doing here
exokem
exokem8mo ago
No description
exokem
exokem8mo ago
MethodBuilder does not seem to provide an override for GetMethodBody, and none is provided by any of its base types so it throws an exception what's your use case for generating IL? there may be a better API
Finnland
Finnland8mo ago
its the compiled result of a programming language im writing
exokem
exokem8mo ago
are you very familiar with IL? I have worked on something similar where I just manually generated the IL
Finnland
Finnland8mo ago
not at all i was having a lot of trouble trying to learn how to generate it manually, because i could only find code like this i was hoping that being able to print the result would help me learn if you have any rescources or advice for generating it manually i would appreciate it
exokem
exokem8mo ago
https://www.codeproject.com/Articles/3778/Introduction-to-IL-Assembly-Language this is what I used to learn how IL works there are also a couple of really annoyingly difficult to find tools for verifying and compiling IL into an executable
CodeProject
Introduction to IL Assembly Language
Start using IL Assembly Language to debug your code at low level and understand how .NET deals with your high level code
exokem
exokem8mo ago
PEVerify or ILVerify for verifying an executable/dll ILAsm for compiling executables/dlls IL is object oriented and you can use the mscorlib for string operations (and various other things)
Finnland
Finnland8mo ago
ok tysm!
exokem
exokem8mo ago
if you run into any specific problems or have questions feel free to dm or reply here (for as long as this thread will be open)
Finnland
Finnland8mo ago
ok thanks i appreciate it idk how long i have rn but i may post anothger thread some other time
Accord
Accord7mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts