private MethodDefinition AddCreateInstanceMethod(TypeWrapper typeWrapper, TypeDefinition type) { MethodDefinition item = new MethodDefinition("CreateInstance", MethodAttributes.CompilerControlled | MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.HideBySig | MethodAttributes.Virtual, this.objectType); MethodBody body = item.Body; ILProcessor iLProcessor = body.GetILProcessor(); if (typeWrapper.Type.IsValueType) { body.InitLocals = true; VariableDefinition definition2 = new VariableDefinition("instance", typeWrapper.Type); body.Variables.Add(definition2); iLProcessor.EmitLdloca(definition2); iLProcessor.Emit(OpCodes.Initobj, typeWrapper.Type); iLProcessor.EmitLdloc(definition2); iLProcessor.Emit(OpCodes.Box, typeWrapper.Type); } else { if (typeWrapper.SpecialConstructor == null) { throw new Exception(string.Format("CreateInstance method can't be added for type \"{0}\".", typeWrapper.Type.FullName)); } iLProcessor.EmitLdc_I4(0); iLProcessor.Emit(OpCodes.Conv_U); iLProcessor.Emit(OpCodes.Newobj, typeWrapper.SpecialConstructor); } iLProcessor.Emit(OpCodes.Ret); type.Methods.Add(item); return(item); }