static int _m_emitPropertyWrap(RealStatePtr L) { try { ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L); XLua.CodeEmit gen_to_be_invoked = (XLua.CodeEmit)translator.FastGetCSObj(L, 1); { System.Reflection.Emit.TypeBuilder _typeBuilder = (System.Reflection.Emit.TypeBuilder)translator.GetObject(L, 2, typeof(System.Reflection.Emit.TypeBuilder)); System.Reflection.PropertyInfo _prop = (System.Reflection.PropertyInfo)translator.GetObject(L, 3, typeof(System.Reflection.PropertyInfo)); System.Reflection.MethodInfo _op = (System.Reflection.MethodInfo)translator.GetObject(L, 4, typeof(System.Reflection.MethodInfo)); bool _genGetter = LuaAPI.lua_toboolean(L, 5); System.Reflection.Emit.MethodBuilder gen_ret = gen_to_be_invoked.emitPropertyWrap(_typeBuilder, _prop, _op, _genGetter); translator.Push(L, gen_ret); return(1); } } catch (System.Exception gen_e) { return(LuaAPI.luaL_error(L, "c# exception:" + gen_e)); } }
/// <summary> /// Gera as propriedades para o tipo que será criado. /// </summary> /// <param name="tb"></param> /// <param name="properties"></param> /// <returns></returns> private System.Reflection.FieldInfo[] GenerateProperties(System.Reflection.Emit.TypeBuilder tb, DynamicProperty[] properties) { var fields = new System.Reflection.Emit.FieldBuilder[properties.Length]; for (int i = 0; i < properties.Length; i++) { DynamicProperty dp = properties[i]; var fb = tb.DefineField("_" + dp.Name, dp.Type, System.Reflection.FieldAttributes.Private); var pb = tb.DefineProperty(dp.Name, System.Reflection.PropertyAttributes.HasDefault, dp.Type, null); var mbGet = tb.DefineMethod("get_" + dp.Name, System.Reflection.MethodAttributes.Public | System.Reflection.MethodAttributes.SpecialName | System.Reflection.MethodAttributes.HideBySig, dp.Type, Type.EmptyTypes); var genGet = mbGet.GetILGenerator(); genGet.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); genGet.Emit(System.Reflection.Emit.OpCodes.Ldfld, fb); genGet.Emit(System.Reflection.Emit.OpCodes.Ret); var mbSet = tb.DefineMethod("set_" + dp.Name, System.Reflection.MethodAttributes.Public | System.Reflection.MethodAttributes.SpecialName | System.Reflection.MethodAttributes.HideBySig, null, new Type[] { dp.Type }); var genSet = mbSet.GetILGenerator(); genSet.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); genSet.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); genSet.Emit(System.Reflection.Emit.OpCodes.Stfld, fb); genSet.Emit(System.Reflection.Emit.OpCodes.Ret); pb.SetGetMethod(mbGet); pb.SetSetMethod(mbSet); fields[i] = fb; } return(fields); }
public override void Implement(DynamicTypeBuilder config, System.Reflection.Emit.TypeBuilder typeBuilder) { var il = this.AddMethod.GetILGenerator(); ProxyMethodHelper.GenerateBody(il, this._implementation.PropertyGetMethod, this._baseEvent.GetAddMethod()); var il2 = this.RemoveMethod.GetILGenerator(); ProxyMethodHelper.GenerateBody(il2, this._implementation.PropertyGetMethod, this._baseEvent.GetRemoveMethod()); }
/// <summary>Generates the type now.</summary> public void Complete() { if (Builder == null) { return; } _Type = Builder.CreateType(); Builder = null; }
private static bool IsExplicitImplemented(DynamicTypeBuilder config, System.Reflection.Emit.TypeBuilder builder, MethodInfo item) { var explicitMethod = config.DynamicMembers.OfType <DynamicMethod>().FirstOrDefault(p => p.IsExplicitImplementation(item)); if (explicitMethod != null) { builder.DefineMethodOverride(explicitMethod.Method, item); return(true); } return(false); }
public override void Implement(DynamicTypeBuilder config, System.Reflection.Emit.TypeBuilder typeBuilder) { var il = this.PropertyGetMethod.GetILGenerator(); ProxyMethodHelper.GenerateBody(il, this.implementation.PropertyGetMethod, this.baseProperty.GetGetMethod()); if (!this.IsReadOnly) { var il2 = this.PropertySetMethod.GetILGenerator(); ProxyMethodHelper.GenerateBody(il2, this.implementation.PropertyGetMethod, this.baseProperty.GetSetMethod()); } }
/// <summary> /// Cria o método para calcular o hashcode. /// </summary> /// <param name="tb"></param> /// <param name="fields"></param> private void GenerateGetHashCode(System.Reflection.Emit.TypeBuilder tb, System.Reflection.FieldInfo[] fields) { var mb = tb.DefineMethod("GetHashCode", System.Reflection.MethodAttributes.Public | System.Reflection.MethodAttributes.ReuseSlot | System.Reflection.MethodAttributes.Virtual | System.Reflection.MethodAttributes.HideBySig, typeof(int), Type.EmptyTypes); var gen = mb.GetILGenerator(); gen.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); foreach (var field in fields) { Type ft = field.FieldType; Type ct = typeof(EqualityComparer <>).MakeGenericType(ft); gen.EmitCall(System.Reflection.Emit.OpCodes.Call, ct.GetMethod("get_Default"), null); gen.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); gen.Emit(System.Reflection.Emit.OpCodes.Ldfld, field); gen.EmitCall(System.Reflection.Emit.OpCodes.Callvirt, ct.GetMethod("GetHashCode", new Type[] { ft }), null); gen.Emit(System.Reflection.Emit.OpCodes.Xor); } gen.Emit(System.Reflection.Emit.OpCodes.Ret); }
private static bool IsVisibleAsClass(Type type) { while (type.HasElementType) { if (type.IsPointer || type.IsByRef) { return(false); } type = type.GetElementType(); } if (type.ContainsGenericParameters && !type.IsGenericTypeDefinition) { return(false); } System.Reflection.Emit.TypeBuilder tb = type as System.Reflection.Emit.TypeBuilder; if (tb != null && !tb.IsCreated()) { return(false); } return(true); }
/// <summary> /// Compile the Binder methods /// </summary> /// <param name="engine">ScriptEngine</param> /// <param name="typeBuilder">The TypeBuilder to attach the compiled Binder methods to</param> public void Compile(ScriptEngine engine, System.Reflection.Emit.TypeBuilder typeBuilder) { int length = this.HasParamArray ? Binder.MaximumCachedParameterCount : this.FunctionLength; for (int argumentCount = 0; argumentCount <= length; argumentCount++) { string compiledName = GetCompiledName(argumentCount); if (!engine.compiledBinders.Contains(compiledName)) { engine.compiledBinders.Add(compiledName); var methodBuilder = typeBuilder.DefineMethod( compiledName, System.Reflection.MethodAttributes.HideBySig | System.Reflection.MethodAttributes.Static | System.Reflection.MethodAttributes.Public, typeof(object), new Type[] { typeof(ScriptEngine), typeof(object), typeof(object[]) }); // Generate the IL for the method. ILGenerator generator = new ReflectionEmitILGenerator(methodBuilder.GetILGenerator()); GenerateStub(generator, argumentCount); } } }
/// <summary> /// Cria o método de comparação Equals. /// </summary> /// <param name="tb"></param> /// <param name="fields"></param> public void GenerateEquals(System.Reflection.Emit.TypeBuilder tb, System.Reflection.FieldInfo[] fields) { var mb = tb.DefineMethod("Equals", System.Reflection.MethodAttributes.Public | System.Reflection.MethodAttributes.ReuseSlot | System.Reflection.MethodAttributes.Virtual | System.Reflection.MethodAttributes.HideBySig, typeof(bool), new Type[] { typeof(object) }); var gen = mb.GetILGenerator(); var other = gen.DeclareLocal(tb); var next = gen.DefineLabel(); gen.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); gen.Emit(System.Reflection.Emit.OpCodes.Isinst, tb); gen.Emit(System.Reflection.Emit.OpCodes.Stloc, other); gen.Emit(System.Reflection.Emit.OpCodes.Ldloc, other); gen.Emit(System.Reflection.Emit.OpCodes.Brtrue_S, next); gen.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); gen.Emit(System.Reflection.Emit.OpCodes.Ret); gen.MarkLabel(next); foreach (var field in fields) { Type ft = field.FieldType; Type ct = typeof(EqualityComparer <>).MakeGenericType(ft); next = gen.DefineLabel(); gen.EmitCall(System.Reflection.Emit.OpCodes.Call, ct.GetMethod("get_Default"), null); gen.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); gen.Emit(System.Reflection.Emit.OpCodes.Ldfld, field); gen.Emit(System.Reflection.Emit.OpCodes.Ldloc, other); gen.Emit(System.Reflection.Emit.OpCodes.Ldfld, field); gen.EmitCall(System.Reflection.Emit.OpCodes.Callvirt, ct.GetMethod("Equals", new Type[] { ft, ft }), null); gen.Emit(System.Reflection.Emit.OpCodes.Brtrue_S, next); gen.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); gen.Emit(System.Reflection.Emit.OpCodes.Ret); gen.MarkLabel(next); } gen.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_1); gen.Emit(System.Reflection.Emit.OpCodes.Ret); }
internal Prototype(ScriptEngine engine, string name, Prototype baseProto, bool isStatic) { Engine = engine; BasePrototype = baseProto; IsStatic = isStatic; ReflectionEmitModuleInfo moduleInfo = engine.ReflectionEmitInfo; if (name == null) { name = "__proto__" + moduleInfo.TypeCount; moduleInfo.TypeCount++; } Name = name; Type baseType; if (baseProto == null) { baseType = typeof(object); } else { baseType = baseProto.Type; } // Define the type now; note that methods are not declared on these (JS defined methods are always static): Builder = moduleInfo.ModuleBuilder.DefineType(name, System.Reflection.TypeAttributes.Public | System.Reflection.TypeAttributes.Class, baseType ); if (!isStatic) { // Define the default ctr and obtain it: AddConstructor(Builder.DefineDefaultConstructor(System.Reflection.MethodAttributes.Public)); } }
static void Main(string[] args) { System.Reflection.Emit.AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new System.Reflection.AssemblyName("Patch"), System.Reflection.Emit.AssemblyBuilderAccess.Run, (string)null); System.Reflection.Emit.ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("module"); System.Reflection.Emit.TypeBuilder typeBuilder = moduleBuilder.DefineType("program", System.Reflection.TypeAttributes.Public); System.Reflection.Emit.MethodBuilder methodBuilder = typeBuilder.DefineMethod("simpleSub", System.Reflection.MethodAttributes.Static | System.Reflection.MethodAttributes.Public | System.Reflection.MethodAttributes.HideBySig, typeof(int), new Type[] { typeof(int), typeof(int) }); List <MSIL.Instruction> patchedMethod = new List <MSIL.Instruction>(); foreach (MSIL.Instruction instruction in MSIL.ReadMethod(typeof(Program).GetMethod("simpleAdd"))) { if (instruction.OpCode == System.Reflection.Emit.OpCodes.Add) { instruction.OpCode = System.Reflection.Emit.OpCodes.Sub; } patchedMethod.Add(instruction); } MSIL.EmitMethod(patchedMethod, methodBuilder); Type type = typeBuilder.CreateType(); System.Reflection.MethodInfo pathedMethod = type.GetMethod("simpleSub"); System.Console.WriteLine("result: " + (int)pathedMethod.Invoke(null, new object[] { 3, 1 })); }
public static Type AsType(System.Reflection.Emit.TypeBuilder type) { return(type); }
public static Type CreateType(this System.Reflection.Emit.TypeBuilder type) { return(type.CreateTypeInfo().AsType()); }
/// <summary> /// Dynamic genereate collection instance specify object map , new instance has /// fields member , those fields's name is key of map , field value is value of map. /// </summary> /// <param name="objs">object maps</param> /// <param name="BaseType">base type</param> /// <returns>dynamic genereate instance</returns> public static object CreateAllObject(System.Collections.Hashtable objs, System.Type BaseType) { if (objs == null) { return(null); } System.Reflection.AssemblyName an = new System.Reflection.AssemblyName(); an.Name = "XDesignerLib_AllObject_Assembly"; System.Reflection.Emit.AssemblyBuilder ab = System.AppDomain.CurrentDomain.DefineDynamicAssembly( an, System.Reflection.Emit.AssemblyBuilderAccess.Run); System.Reflection.Emit.ModuleBuilder mb = ab.DefineDynamicModule("AllObjectModule"); System.Reflection.Emit.TypeBuilder tb = null; if (BaseType == null) { tb = mb.DefineType( "AllObject" + System.Environment.TickCount, System.Reflection.TypeAttributes.Public); } else { tb = mb.DefineType( "AllObject" + System.Environment.TickCount, System.Reflection.TypeAttributes.Public, BaseType); } System.Collections.ArrayList Fields = new System.Collections.ArrayList(); foreach (string key in objs.Keys) { if (key != null && key.Length != 0) { bool bolFind = false; foreach (System.Reflection.FieldInfo f in Fields) { if (string.Compare(f.Name, key, true) == 0) { bolFind = true; break; } } if (bolFind == false) { Fields.Add(tb.DefineField( key, typeof(object), System.Reflection.FieldAttributes.Public)); } } }//foreach System.Type t = tb.CreateType(); object obj = System.Activator.CreateInstance(t); foreach (System.Reflection.FieldInfo field in Fields) { t.InvokeMember( field.Name, System.Reflection.BindingFlags.SetField, null, obj, new object[] { objs[field.Name] }); }//foreach return(obj); }
public static System.CodeDom.Compiler.CompilerErrorCollection CompileToType(System.Xml.XmlReader stylesheet, XsltSettings settings, System.Xml.XmlResolver stylesheetResolver, bool debug, System.Reflection.Emit.TypeBuilder typeBuilder, string scriptAssemblyPath) { }
public void Intercept(DynamicTypeBuilder config, System.Reflection.Emit.TypeBuilder builder) { builder.AddInterfaceImplementation(this.InterfaceType); var baseMethods = builder.BaseType.GetMethods().ToList(); var baseEvents = builder.BaseType.GetEvents(); var baseProperties = builder.BaseType.GetProperties(); var baseAccessorMethods = baseEvents.Select(p => p.GetAddMethod()).Union( baseEvents.Select(p => p.GetRemoveMethod())).Union( baseProperties.Select(p => p.GetSetMethod())).Union( baseProperties.Select(p => p.GetGetMethod())).Where(p => p != null); var interfaceProperties = this.InterfaceType.GetProperties(); var interfaceEvents = this.InterfaceType.GetEvents(); var interfaceAccessorMethods = interfaceEvents.Select(p => p.GetAddMethod()).Union( interfaceEvents.Select(p => p.GetRemoveMethod())).Union( interfaceProperties.Select(p => p.GetSetMethod())).Union( interfaceProperties.Select(p => p.GetGetMethod())).Where(p => p != null); foreach (var item in this.InterfaceType.GetMethods()) { if (!IsExplicitImplemented(config, builder, item)) { if (!interfaceAccessorMethods.Contains(item)) { var method = config.DynamicMembers.OfType <DynamicMethod>() .Where(p => p.MemberEquals(item)) .Select(p => p.Method) .OfType <MethodInfo>() .FirstOrDefault(); if (method == null) { method = baseMethods.Where(p => p.Name == item.Name && p.ReturnType == item.ReturnType && p.GetParameters().Length == item.GetParameters().Length&& p.GetParameters().Select((x, i) => new { Index = i, Value = x.ParameterType }).All(x => item.GetParameters()[x.Index].ParameterType == x.Value)) .FirstOrDefault(); } if (method == null) { #if NETSTANDARD1_6 throw new MissingMemberException($"Missiong member {item.Name} on type {builder.Name}."); #else throw new MissingMemberException(builder.Name, item.Name); #endif } builder.DefineMethodOverride(method, item); } } } foreach (var item in this.InterfaceType.GetProperties()) { var get = item.GetGetMethod(); if (get != null && IsExplicitImplemented(config, builder, get)) { get = null; } var set = item.GetSetMethod(); if (set != null && IsExplicitImplemented(config, builder, set)) { set = null; } if (get == null && set == null) { continue; } var prop = config.DynamicMembers.OfType <DynamicProperty>().Where(p => p.MemberName == item.Name).FirstOrDefault(); if (prop != null) { if (get != null) { builder.DefineMethodOverride(prop.PropertyGetMethod, get); } if (set != null) { builder.DefineMethodOverride(prop.PropertySetMethod, set); } } else { var baseProp = baseProperties.Where(p => p.Name == item.Name).FirstOrDefault(); if (baseProp == null) { #if NETSTANDARD1_6 throw new MissingMemberException($"Missiong member {item.Name} on type {builder.Name}."); #else throw new MissingMemberException(builder.Name, item.Name); #endif } if (get != null) { builder.DefineMethodOverride(baseProp.GetGetMethod(), get); } if (set != null) { builder.DefineMethodOverride(baseProp.GetSetMethod(), set); } } } foreach (var item in this.InterfaceType.GetEvents()) { var add = item.GetAddMethod(); if (add != null && IsExplicitImplemented(config, builder, add)) { add = null; } var remove = item.GetRemoveMethod(); if (remove != null && IsExplicitImplemented(config, builder, remove)) { remove = null; } if (add == null && remove == null) { continue; } var evt = config.DynamicMembers.OfType <DynamicEvent>().Where(p => p.MemberName == item.Name).FirstOrDefault(); if (evt != null) { if (add != null) { builder.DefineMethodOverride(evt.AddMethod, add); } if (remove != null) { builder.DefineMethodOverride(evt.RemoveMethod, remove); } } else { var baseEvent = baseEvents.Where(p => p.Name == item.Name).FirstOrDefault(); if (baseEvent == null) { #if NETSTANDARD1_6 throw new MissingMemberException($"Missiong member {item.Name} on type {builder.Name}."); #else throw new MissingMemberException(builder.Name, item.Name); #endif } if (add != null) { builder.DefineMethodOverride(baseEvent.GetAddMethod(), add); } if (remove != null) { builder.DefineMethodOverride(baseEvent.GetRemoveMethod(), remove); } } } }
public static Type AsType(System.Reflection.Emit.TypeBuilder type) { return(type == null ? null : type.AsType()); }
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); }
internal Assembly DoTypeBuilderResolve(System.Reflection.Emit.TypeBuilder tb) { return(null); }