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; }
}
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:
To invoke, I just extracted the contents of the KVP here: