public ConstructorGenerator(System.Reflection.Emit.ConstructorBuilder builder, ParameterInfo[] parameters, TypeGenerator generator) { this.builder = builder; Name = builder.Name; Parameters = parameters; Declaring = generator; Context = generator.Context; Attributes = builder.Attributes; }
public ConstructorBuilder(System.Reflection.Emit.ConstructorBuilder builder) { _builder = builder; _il = new ILExpressed(builder.GetILGenerator()); }
static System.Type GenerateFunctionType(System.Type functionType) { SQLiteFunctionAttribute attribute = AttributeExtensions.GetCustomAttribute <SQLiteFunctionAttribute>(functionType); if (attribute == null) { return(null); } bool ex = TypeExtensions.IsInheritFrom(functionType, typeof(SQLiteFunctionEx)) || attribute.Type == FunctionTypes.Collation; FastWrapper baseType = GetType(ex ? "System.Data.SQLite.SQLiteFunctionEx" : "System.Data.SQLite.SQLiteFunction"); System.Reflection.AssemblyName assemblyName = new System.Reflection.AssemblyName(functionType.Namespace + ".DynamicClass_" + functionType.Name); System.Reflection.Emit.AssemblyBuilderAccess accemblyBuilderAccess = #if netcore System.Reflection.Emit.AssemblyBuilderAccess.Run; #else IsDebug ? System.Reflection.Emit.AssemblyBuilderAccess.RunAndSave : System.Reflection.Emit.AssemblyBuilderAccess.Run; #endif System.Reflection.Emit.AssemblyBuilder assembly = #if netcore System.Reflection.Emit.AssemblyBuilder.DefineDynamicAssembly(assemblyName, accemblyBuilderAccess); #else System.AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, accemblyBuilderAccess); #endif #if !netcore bool canSave = (accemblyBuilderAccess == System.Reflection.Emit.AssemblyBuilderAccess.RunAndSave || accemblyBuilderAccess == System.Reflection.Emit.AssemblyBuilderAccess.Save); #endif System.Reflection.Emit.ModuleBuilder module = #if netcore assembly.DefineDynamicModule(assemblyName.Name); #else canSave ? assembly.DefineDynamicModule(assemblyName.Name, assemblyName.Name + ".dll") : assembly.DefineDynamicModule(assemblyName.Name);//, n.Name + ".dll"); #endif System.Reflection.Emit.TypeBuilder type = module.DefineType( assemblyName.Name + ".DynamicClass", System.Reflection.TypeAttributes.Public | System.Reflection.TypeAttributes.Sealed | System.Reflection.TypeAttributes.AutoClass, baseType.Type, System.Type.EmptyTypes); { FastWrapper wrapper = GetType("System.Data.SQLite.SQLiteFunctionAttribute"); System.Reflection.PropertyInfo[] properties = new System.Reflection.PropertyInfo[] { wrapper.Type.GetProperty("Name"), wrapper.Type.GetProperty("Arguments"), wrapper.Type.GetProperty("FuncType"), }; System.Reflection.Emit.CustomAttributeBuilder attributeBuilder = new System.Reflection.Emit.CustomAttributeBuilder(wrapper.Type.GetConstructor(System.Type.EmptyTypes), new object[0], properties, new object[] { attribute.Name, attribute.Arguments, TypeExtensions.Convert(attribute.Type, GetType("System.Data.SQLite.FunctionType").Type), }); type.SetCustomAttribute(attributeBuilder); } System.Reflection.Emit.FieldBuilder _o = type.DefineField("_o", functionType, FieldAttributes.Private); { System.Reflection.Emit.ConstructorBuilder ctor = type.DefineConstructor( System.Reflection.MethodAttributes.Public | System.Reflection.MethodAttributes.HideBySig | System.Reflection.MethodAttributes.SpecialName | System.Reflection.MethodAttributes.RTSpecialName, System.Reflection.CallingConventions.HasThis, System.Type.EmptyTypes); System.Reflection.Emit.ILGenerator il = ctor.GetILGenerator(); il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); il.Emit(System.Reflection.Emit.OpCodes.Call, baseType.Type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance | BindingFlags.Instance, null, System.Type.EmptyTypes, new System.Reflection.ParameterModifier[0])); il.Emit(System.Reflection.Emit.OpCodes.Nop); il.Emit(System.Reflection.Emit.OpCodes.Nop); il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); il.Emit(System.Reflection.Emit.OpCodes.Newobj, functionType.GetConstructor(System.Type.EmptyTypes)); il.Emit(System.Reflection.Emit.OpCodes.Stfld, _o); il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); il.Emit(System.Reflection.Emit.OpCodes.Ldfld, _o); il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); if (attribute.Type == FunctionTypes.Collation) { il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_1); } else { il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); } il.Emit(System.Reflection.Emit.OpCodes.Callvirt, functionType.GetMethod("Init", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, new System.Type[] { typeof(object), typeof(bool) }, null)); il.Emit(System.Reflection.Emit.OpCodes.Nop); il.Emit(System.Reflection.Emit.OpCodes.Ret); } CreateMethodDelegate createMethod = (methodInfo, action) => { System.Reflection.ParameterInfo[] parameters = methodInfo.GetParameters(); System.Type[] parameterTypes = new System.Type[parameters.Length]; for (int i = 0; i < parameters.Length; i++) { parameterTypes[i] = parameters[i].ParameterType; } System.Reflection.Emit.MethodBuilder method = type.DefineMethod(methodInfo.Name, (methodInfo.Attributes | MethodAttributes.NewSlot) ^ MethodAttributes.NewSlot, methodInfo.CallingConvention, methodInfo.ReturnType, parameterTypes); for (int i = 0; i < parameters.Length; i++) { System.Reflection.Emit.ParameterBuilder parameter = method.DefineParameter(i + 1, parameters[i].Attributes, parameters[i].Name); if (parameters[i].IsOptional) { if (parameters[i].ParameterType.IsValueType && parameters[i].DefaultValue == null) { continue; } parameter.SetConstant(parameters[i].DefaultValue); } } System.Reflection.Emit.ILGenerator il = method.GetILGenerator(); bool hasReturn = (methodInfo.ReturnType != typeof(void)); System.Reflection.Emit.LocalBuilder @return = null; if (hasReturn) { @return = il.DeclareLocal(methodInfo.ReturnType); } il.Emit(System.Reflection.Emit.OpCodes.Nop); il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); il.Emit(System.Reflection.Emit.OpCodes.Ldfld, _o); action(functionType.GetMethod(methodInfo.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance), method, il); il.Emit(System.Reflection.Emit.OpCodes.Ret); }; if (attribute.Type == FunctionTypes.Scalar) { createMethod(baseType.Type.GetMethod("Invoke"), (methodInfo, method, il) => { il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); il.Emit(System.Reflection.Emit.OpCodes.Callvirt, methodInfo); il.Emit(System.Reflection.Emit.OpCodes.Stloc_0); System.Reflection.Emit.Label label = il.DefineLabel(); il.Emit(System.Reflection.Emit.OpCodes.Br_S, label); il.MarkLabel(label); il.Emit(System.Reflection.Emit.OpCodes.Ldloc_0); }); } else if (attribute.Type == FunctionTypes.Collation) { createMethod(baseType.Type.GetMethod("Compare"), (methodInfo, method, il) => { il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); il.Emit(System.Reflection.Emit.OpCodes.Ldarg_2); il.Emit(System.Reflection.Emit.OpCodes.Callvirt, methodInfo); il.Emit(System.Reflection.Emit.OpCodes.Stloc_0); System.Reflection.Emit.Label label = il.DefineLabel(); il.Emit(System.Reflection.Emit.OpCodes.Br_S, label); il.MarkLabel(label); il.Emit(System.Reflection.Emit.OpCodes.Ldloc_0); }); } else { createMethod(baseType.Type.GetMethod("Final"), (methodInfo, method, il) => { il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); il.Emit(System.Reflection.Emit.OpCodes.Callvirt, methodInfo); il.Emit(System.Reflection.Emit.OpCodes.Stloc_0); System.Reflection.Emit.Label label = il.DefineLabel(); il.Emit(System.Reflection.Emit.OpCodes.Br_S, label); il.MarkLabel(label); il.Emit(System.Reflection.Emit.OpCodes.Ldloc_0); }); createMethod(baseType.Type.GetMethod("Step"), (methodInfo, method, il) => { il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); il.Emit(System.Reflection.Emit.OpCodes.Ldarg_2); il.Emit(System.Reflection.Emit.OpCodes.Ldarg_3); il.Emit(System.Reflection.Emit.OpCodes.Callvirt, methodInfo); il.Emit(System.Reflection.Emit.OpCodes.Nop); }); } { System.Reflection.MethodInfo methodInfo_base = baseType.Type.GetMethod("Dispose", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, new System.Type[] { typeof(bool) }, null); createMethod(methodInfo_base, (methodInfo, method, il) => { il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); il.Emit(System.Reflection.Emit.OpCodes.Callvirt, methodInfo); il.Emit(System.Reflection.Emit.OpCodes.Nop); il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); il.Emit(System.Reflection.Emit.OpCodes.Call, methodInfo_base); il.Emit(System.Reflection.Emit.OpCodes.Nop); }); } #if netcore20 var result = type.CreateTypeInfo(); #else var result = type.CreateType(); #endif #if !netcore if (canSave) { assembly.Save(assemblyName.Name + ".dll"); } #endif return(result); }