How to Emit code to assign value/reference to static field of class by calling it's constructor?

(My code is somewhat a mess of C# and VB.NET) I am trying to Emit class that looks as following:

public class SWTTFields
{
    private string fieldName;
    private int startPosition;
    private int endPosition;

    public static readonly SWTTFields ISO = new SWTTFields("ISO", 1, 2);
    public static readonly SWTTFields EPC = new SWTTFields("EPC", 3, 4);

    private SWTTFields(String fieldName, Int32 startPositon, Int32 endPositon)
    {
        this.fieldName = fieldName;
        this.startPosition = startPositon;
        this.endPosition = endPositon;
    }
}

However, I am stack on assigning references to two static members. So far, I got this point (code below is in VB.NET, however I will gladly accept answers in VB.NET or C#):

Dim typeBuilder As TypeBuilder = GetTypeBuilder()

'Definition of three private variables
'TODO: you need one more private field that would represent "Bank" particular field belongs to

'Private String fieldName
Dim fieldName As FieldBuilder = typeBuilder.DefineField("fieldName",
                                                         GetType(String),
                                                            FieldAttributes.Private)
'Private Int32 startPosition
Dim startPosition As FieldBuilder = typeBuilder.DefineField("startPosition",
                                                            GetType(Int32),
                                                              FieldAttributes.Private)
'Private Int32 endPosition
Dim endPosition As FieldBuilder = typeBuilder.DefineField("endPosition",
                                                          GetType(Int32), FieldAttributes.Private)

'Type is taken from the current TypeBuilder
Dim ISO As FieldBuilder = typeBuilder.DefineField("ISO", typeBuilder.GetType, FieldAttributes.Public)                                                           
Dim EPC As FieldBuilder = typeBuilder.DefineField("EPC", typeBuilder.GetType, FieldAttributes.Public)

'Constructor parameters are
Dim constructorParams() As Type = {GetType(String), GetType(Int32), GetType(Int32)}

'constructor for the class 
Dim ctorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public Or
                                                                     MethodAttributes.SpecialName Or
                                                                     MethodAttributes.RTSpecialName,
                                                                     CallingConventions.Standard,
                                                                     constructorParams)

Dim objType As Type = Type.GetType("System.Object")
'pulls out info about Object constructor. It will be called later 
Dim objCtor As ConstructorInfo = objType.GetConstructor(Type.EmptyTypes)

'Generating code (some of the code and comments came from ConstructorBuilder class)
Dim ctorIL As ILGenerator = ctorBuilder.GetILGenerator()

'place "this" on the stack 
ctorIL.Emit(OpCodes.Ldarg_0)
'Create instance of System.Object by invokign its ctor
ctorIL.Emit(OpCodes.Call, objCtor)

'store fieldName parameter 
ctorIL.Emit(OpCodes.Ldarg_0)
ctorIL.Emit(OpCodes.Ldarg_1)
ctorIL.Emit(OpCodes.Stfld, fieldName)

'store startPosition
ctorIL.Emit(OpCodes.Ldarg_0)
ctorIL.Emit(OpCodes.Ldarg_2)
ctorIL.Emit(OpCodes.Stfld, startPosition)

'stored endPosition
ctorIL.Emit(OpCodes.Ldarg_0)
ctorIL.Emit(OpCodes.Ldarg_3)
ctorIL.Emit(OpCodes.Stfld, endPosition)
'Done. Return
ctorIL.Emit(OpCodes.Ret)
Jon Skeet
people
quotationmark

I haven't used TypeBuilder myself so this is more of a hint than full code, but basically I believe you want TypeBuilder.DefineTypeInitializer, and perform the assignments in there.

In other words, think of the class as looking like this, in terms of its static members:

public class SWTTFields
{
    public static readonly SWTTFields ISO;
    public static readonly SWTTFields EPC;

    static SWTTFields()
    {
        ISO = new SWTTFields("ISO", 1, 2);
        EPC = new SWTTFields("EPC", 3, 4);
    }
}

(That's not exactly equivalent to the original code, due to the difference that a static constructor makes, but it's close enough.)

It looks like you already know how to generate the relevant IL for that type initializer body - just treat it like any other method/constructor body.

people

See more on this question at Stackoverflow