public void Execute() { LogInfo("Applying FastClone..."); foreach (var definition in ModuleDefinition.Types.Where(ImplementsIFastClone)) { if (!TypeInspector.HasParameterlessConstructor(definition)) { LogInfo($"Type {definition.Name} lacks a parameterless constructor, skipping"); continue; } LogInfo($"Extending {definition.Name}"); // Add Static Clone var staticMethod = BuildStaticCloneMethod(definition); // Add Instance Method BuildInstanceMethod(definition, staticMethod); } LogInfo("Done"); }
private MethodDefinition BuildStaticCloneMethod(TypeDefinition target) { var method = new MethodDefinition( StaticCloneMethodName, MethodAttributes.Private | MethodAttributes.Static, target); method.Parameters.Add(new ParameterDefinition( SourceParamName, ParameterAttributes.None, target)); var constructor = TypeInspector.GetParameterlessConstructor(target); var processor = method.Body.GetILProcessor(); processor.Emit(OpCodes.Newobj, constructor); // invoke constructor SetFields(processor, target); processor.Emit(OpCodes.Ret); // Return target.Methods.Add(method); return(method); }