Finnland
Finnland
CC#
Created by Finnland on 11/13/2023 in #help
I cant figure out constructor inheritance
how do i inherit classes, including their constructors? no matter what i try i get a wide variety of different errors. i have tried just:
public class SubClass : Superclass {

}
public class SubClass : Superclass {

}
but then i get the error
'SuperClass' does not contain a constructor that takes 0 arguments
'SuperClass' does not contain a constructor that takes 0 arguments
so then i tried adding
public class SubClass : Superclass {
public SubClass(arg) : base(arg)
{

}
}
public class SubClass : Superclass {
public SubClass(arg) : base(arg)
{

}
}
but that gives me an error that i need a method body. How do i inherit the constructors in addition to the rest of the class?
6 replies
CC#
Created by Finnland on 11/3/2023 in #help
❔ 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)
19 replies