Assign fields in constructor via Reflection Emit

1k Views Asked by At

Looking for resolution on how to assign properties w/ backing fields which I have created dynamically at RT in an instance constructor. The signatures match with compiler generated attributes as auto-properties. Essentially they would be equivalent to the code listed below.

Using .NET Core 2.0

Question: How do I assign the backing fields within the constructor using Emit?

For example:

public class MyClass {
  public MyClass(int f1, string f2) {
    _field1 = f1;
    _field2 = f2;
  }

  private readonly int _field1;
  private readonly string _field2;

  public int Field1 { get; }
  public string Field2 { get; }
}

private static void CreateConstructor(TypeBuilder typeBuilder, IReadOnlyList<dynamic> backingFields) {
  var constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, CallingConventions.Standard, new[] {typeof(KeyValuePair<string, string>), typeof(Dictionary<string, Type>)});
  var ctorIL = constructorBuilder.GetILGenerator();

  // Load the current instance ref in arg 0, along
  // with the value of parameter "x" stored in arg X, into stfld.

  for (var x = 0; x < backingFields.Count; x++) {
    ctorIL.Emit(OpCodes.Ldarg_0);
    ctorIL.Emit(OpCodes.Ldarg_S, x+1);
    ctorIL.Emit(OpCodes.Stfld, backingFields[x]);
  }

  ctorIL.Emit(OpCodes.Ret); 
}

  public .cctor(KeyValuePair<string, string> kvp, Dictionary<string, Type> collection) {
    _Name = kvp.Key;
    _JSON = kvp.Value;
    _PropertyInfo = collection;
  }

Iterate over methods defined within interface and create a new properties & accessors w/ private setters in the new Type.

  public interface IComplexType {
    string Name { get; set; }
    string JSON { get; set; }
    object PropertyInfo { get; set; }
  }
1

There are 1 best solutions below

0
On

Resolved!

Needed to change the constructor arguments to match the number of iterations since it was harder to Ldarg_1 as KeyValuePair and assign its Key & Value respectivly.

By eliminating the KVP and providing an additional parameter the constructor is defined as follows:

  var constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, CallingConventions.Standard, new[] {typeof(string), typeof(string), typeof(Dictionary<string, Type>)});
  var ctorIl = constructorBuilder.GetILGenerator();

  for (var x = 0; x < backingFields.Count; x++) {
    ctorIl.Emit(OpCodes.Ldarg_0);
    ctorIl.Emit(OpCodes.Ldarg_S, x + 1);
    ctorIl.Emit(OpCodes.Stfld, backingFields[x]);
  }

  ctorIl.Emit(OpCodes.Ret);

To invoke, I just extracted the contents of the KVP here:

  return (T) Activator.CreateInstance(TypeCollection[type], kvp.Key, kvp.Value, collection);