internal static MethodDefinition CreateOnSerializing(this ModuleDefinition module, IEnumerable <Instruction> instructions) { var onSerializingMethod = new MethodDefinition("OnSerializing", MethodAttributes.Public, module.TypeSystem.Void); var streamingContext = module.ImportReference(typeof(StreamingContext)); onSerializingMethod.Parameters.Add(new ParameterDefinition("context", ParameterAttributes.None, streamingContext)); var onSerializingAttribute = module.CreateCustomAttribute <OnSerializingAttribute>(); onSerializingMethod.CustomAttributes.Add(onSerializingAttribute); foreach (var instruction in instructions) { onSerializingMethod.Body.Instructions.Add(instruction); } return(onSerializingMethod); }