ConstructorBuilder
ConstructorBuilderRequest is a request to create a ConstructorBuilder.
// Request to create constructor builder
IRequestFor<ConstructorBuilder> ctor = new ConstructorBuilderRequest(
methodAttributes: MethodAttributes.Public,
attributes: new object[] { new PropertyAttribute("Key", "Value") },
callingConventions: null,
parameterTypes: typeof(int));
Opcodes are added to constructor.
// ".ctor(int x) { MyField = x < 5 ? 0 : 1; }"
IRequestFor<ILGenerator> ops = ILGeneratorRequest.Default
.Append(OpCodes.Ldarg_0)
.Append(OpCodes.Ldarg_1)
.Append(OpCodes.Ldc_I4_5)
.Append(OpCodes.Cgt)
.Append(OpCodes.Stfld, field)
.Append(OpCodes.Ret);
// Add ops to .ctor
ctor = ctor.Append(ops);
Constructor is added to type.
// Request to create type builder
IRequestFor<TypeBuilder> typeBuilder = new TypeBuilderRequest("MyClass03", TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.AutoClass);
// Add field to type request
typeBuilder = typeBuilder.Append(field);
// Add .ctor to type request
typeBuilder = typeBuilder.Append(ctor);
Type is instantiated with constructor arguments.
// Request to build type
IRequestFor<Type> type = new TypeRequest(typeBuilder);
// Request to create new instance
IRequestFor<Object> newRequest = type.NewRequest(null, Parameters: 9);
// Get instance
object instance = service.GetRequired<IRequestFor<object>, object>(newRequest);
// Read with reflection
int value = (int)instance.GetType().GetField("MyField")!.GetValue(instance)!;
// Print
Console.WriteLine(value);
Full Example
Full example
using System;
using System.Reflection;
using System.Reflection.Emit;
using Avalanche;
using Avalanche.Emit;
using Avalanche.Service;
public class constructorbuilder
{
public static void Run()
{
{
// Create service
IService service = Services.Create(Avalanche.Emit.Module.Instance);
// <01>
// Request to create constructor builder
IRequestFor<ConstructorBuilder> ctor = new ConstructorBuilderRequest(
methodAttributes: MethodAttributes.Public,
attributes: new object[] { new PropertyAttribute("Key", "Value") },
callingConventions: null,
parameterTypes: typeof(int));
// </01>
// Request to create field
IRequestFor<FieldBuilder> field = new FieldBuilderRequest("MyField", typeof(int), FieldAttributes.Public);
// <02>
// ".ctor(int x) { MyField = x < 5 ? 0 : 1; }"
IRequestFor<ILGenerator> ops = ILGeneratorRequest.Default
.Append(OpCodes.Ldarg_0)
.Append(OpCodes.Ldarg_1)
.Append(OpCodes.Ldc_I4_5)
.Append(OpCodes.Cgt)
.Append(OpCodes.Stfld, field)
.Append(OpCodes.Ret);
// Add ops to .ctor
ctor = ctor.Append(ops);
// </02>
// <03>
// Request to create type builder
IRequestFor<TypeBuilder> typeBuilder = new TypeBuilderRequest("MyClass03", TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.AutoClass);
// Add field to type request
typeBuilder = typeBuilder.Append(field);
// Add .ctor to type request
typeBuilder = typeBuilder.Append(ctor);
// </03>
// <04>
// Request to build type
IRequestFor<Type> type = new TypeRequest(typeBuilder);
// Request to create new instance
IRequestFor<Object> newRequest = type.NewRequest(null, Parameters: 9);
// Get instance
object instance = service.GetRequired<IRequestFor<object>, object>(newRequest);
// Read with reflection
int value = (int)instance.GetType().GetField("MyField")!.GetValue(instance)!;
// Print
Console.WriteLine(value);
// </04>
}
}
}