5
Voglio realizzare un'interfaccia che cancella automaticamente tutti i campi locali, finora ho:Come si emetterà il valore predefinito di un tipo?
// Implement IClearable
dynamicType.AddInterfaceImplementation(typeof(IClearable));
MethodBuilder clearnMethodBuilder = dynamicType.DefineMethod("Clear", MethodAttributes.Public | MethodAttributes.Virtual, CallingConventions.Standard);
ILGenerator clearMethodILGen = clearnMethodBuilder.GetILGenerator();
foreach (FieldBuilder localField in fields)
{
clearMethodILGen.Emit(OpCodes.Ldarg_0);
clearMethodILGen.Emit(OpCodes.Ldfld, localField);
clearMethodILGen.Emit(OpCodes.??, Profit??);
}
clearMethodILGen.Emit(OpCodes.Ret);
Come impostare l'ultimo passo per salvare il valore di default sul campo?
È questo interesse? http://stackoverflow.com/questions/10400358/how-to-translate-defaultsometype-from-c-sharp-to-cil –
Yup che aiuta. In realtà, sto scoprendo che il metodo corretto potrebbe essere quello di emettere Activator.CreateInstance (T). Dal momento che questo istanzia automaticamente l'oggetto predefinito. – sircodesalot
classe o struct? Se quest'ultimo, puoi fare 'this = new StructName()'. – leppie