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); }
/// <summary> /// Ends a locking-block scope before the given <paramref name="endInstruction"/>. /// </summary> /// <param name="endInstruction">The <see cref="Instruction"/> directly following the lock scope.</param> public void EndLock(Instruction endInstruction) { var endFinally = _processor.Create(OpCodes.Endfinally); var leaveTry = _processor.Create(OpCodes.Leave_S, endInstruction); _processor.Append(leaveTry); var startFinally = _processor.CreateLdloc(_lockCheckLocal); _processor.Append(startFinally); _processor.Emit(OpCodes.Brfalse_S, endFinally); _processor.EmitLdloc(_lockLocal); MethodReference monitorExit = _module.ImportReference(GetMonitorExit(_module)); _processor.Emit(OpCodes.Call, monitorExit); _processor.Append(endFinally); _processor.Append(endInstruction); var handler = new ExceptionHandler(ExceptionHandlerType.Finally) { TryStart = _tryBegin, TryEnd = startFinally, HandlerStart = startFinally, HandlerEnd = endInstruction }; _method.Body.ExceptionHandlers.Add(handler); _tryBegin = null; }
private void AddLoadMethod(TypeDefinition type, FieldDefinition dataField) { MethodDefinition item = new MethodDefinition("Load", MethodAttributes.CompilerControlled | MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.HideBySig | MethodAttributes.Static, base.ModuleContext.IntPtrType); type.Methods.Add(item); MethodBody body = item.Body; ILProcessor iLProcessor = body.GetILProcessor(); TypeReference corLibType = base.ModuleContext.GetCorLibType("System.Byte"); TypeReference variableType = base.ModuleContext.GetCorLibType("System.Int32"); TypeReference reference3 = base.ModuleContext.GetCorLibType("System.IntPtr"); TypeReference reference4 = base.ModuleContext.GetCorLibType("System.Runtime.InteropServices.Marshal"); TypeReference reference5 = base.ModuleContext.GetCorLibType("System.IO.Stream"); TypeReference reference6 = base.ModuleContext.GetCorLibType("System.Type"); VariableDefinition definition2 = new VariableDefinition("stream", reference5); VariableDefinition definition3 = new VariableDefinition("length", variableType); VariableDefinition definition4 = new VariableDefinition("buffer", new ArrayType(corLibType)); VariableDefinition definition5 = new VariableDefinition("result", reference3); body.Variables.Add(definition2); body.Variables.Add(definition3); body.Variables.Add(definition4); body.Variables.Add(definition5); body.InitLocals = true; iLProcessor.Emit(OpCodes.Ldtoken, type); iLProcessor.Emit(OpCodes.Call, base.ModuleContext.GetCorLibMethod(reference6, "GetTypeFromHandle")); if (base.OperationContext.IsWSA) { iLProcessor.Emit(OpCodes.Call, base.ModuleContext.GetCorLibMethod("System.Reflection.IntrospectionExtensions", "GetTypeInfo")); iLProcessor.Emit(OpCodes.Callvirt, base.ModuleContext.GetCorLibMethod("System.Reflection.TypeInfo", "get_Assembly")); } else { iLProcessor.Emit(OpCodes.Callvirt, base.ModuleContext.GetCorLibMethod(reference6, "get_Assembly")); } iLProcessor.Emit(OpCodes.Ldstr, "UnityMetadata"); iLProcessor.Emit(OpCodes.Callvirt, base.ModuleContext.GetCorLibMethod("System.Reflection.Assembly", <> c.< > 9__2_0 ?? (< > c.< > 9__2_0 = new Func <MethodDefinition, bool>(< > c.< > 9. < AddLoadMethod > b__2_0)))); iLProcessor.EmitStloc(definition2); iLProcessor.EmitLdloc(definition2); iLProcessor.Emit(OpCodes.Callvirt, base.ModuleContext.GetCorLibMethod(reference5, "get_Length")); iLProcessor.Emit(OpCodes.Conv_I4); iLProcessor.EmitStloc(definition3); iLProcessor.EmitLdloc(definition3); iLProcessor.Emit(OpCodes.Newarr, corLibType); iLProcessor.EmitStloc(definition4); iLProcessor.EmitLdloc(definition2); iLProcessor.EmitLdloc(definition4); iLProcessor.EmitLdc_I4(0); iLProcessor.EmitLdloc(definition3); iLProcessor.Emit(OpCodes.Callvirt, base.ModuleContext.GetCorLibMethod(reference5, "Read")); iLProcessor.Emit(OpCodes.Pop); iLProcessor.EmitLdloc(definition2); iLProcessor.Emit(OpCodes.Callvirt, base.ModuleContext.GetCorLibMethod(reference5, <> c.< > 9__2_1 ?? (< > c.< > 9__2_1 = new Func <MethodDefinition, bool>(< > c.< > 9. < AddLoadMethod > b__2_1)))); iLProcessor.EmitLdloc(definition3); iLProcessor.Emit(OpCodes.Call, base.ModuleContext.GetCorLibMethod(reference4, "AllocCoTaskMem")); iLProcessor.EmitStloc(definition5); iLProcessor.EmitLdloc(definition4); iLProcessor.EmitLdc_I4(0); iLProcessor.EmitLdloc(definition5); iLProcessor.EmitLdloc(definition3); iLProcessor.Emit(OpCodes.Call, base.ModuleContext.GetCorLibMethod(reference4, <> c.< > 9__2_2 ?? (< > c.< > 9__2_2 = new Func <MethodDefinition, bool>(< > c.< > 9. < AddLoadMethod > b__2_2)))); foreach (AssemblyWrapper wrapper in base.MetadataContainer.Assemblies) { if (wrapper.GetUnityTypeMethod != null) { iLProcessor.EmitLdloc(definition5); iLProcessor.EmitLdc_I4(wrapper.Offset); iLProcessor.Emit(OpCodes.Add); iLProcessor.Emit(OpCodes.Ldftn, base.ModuleContext.Import(wrapper.GetUnityTypeMethod)); iLProcessor.Emit(OpCodes.Stind_I); } } iLProcessor.EmitLdloc(definition5); iLProcessor.Emit(OpCodes.Stsfld, dataField); iLProcessor.EmitLdloc(definition5); iLProcessor.Emit(OpCodes.Ret); }