I trying to emit dynamic method that sets fields of class by some values and when type of regular field is struct I want to set field with new struct like this: var myStruct = new SomeStruct( );.
But I can’t to find default constructor of that struct.
var type = valueForField.GetType ( );
if( type.IsValueType && !type.IsPrimitive && !type.IsEnum )
{
emit
.ldarg_0
.newobj ( type.GetConstructor( Type.EmptyTypes ) )
.stfld ( field );
continue;
}
It fails at line .newobj ( type.GetConstructor( Type.EmptyTypes ) ) because of passing null to newobj function
Can anybody tell me how to emit new structure creation through it default constructor?
Use initobj
From msdn: