public ILLocalVariable(System.Reflection.Emit.LocalBuilder local, string name) { UnderlyingLocal = local; Name = name; }
public virtual void EmitWriteLine(System.Reflection.Emit.LocalBuilder localBuilder) { }
internal CreateXmlSerializableDelegate GenerateCreateXmlSerializableDelegate() { Type type = this.UnderlyingType; CodeGenerator ilg = new CodeGenerator(); bool memberAccessFlag = RequiresMemberAccessForCreate(null) && !(type.FullName == "System.Xml.Linq.XElement"); try { ilg.BeginMethod("Create" + DataContract.GetClrTypeFullName(type), typeof(CreateXmlSerializableDelegate), memberAccessFlag); } catch (SecurityException securityException) { if (memberAccessFlag) { RequiresMemberAccessForCreate(securityException); } else { throw; } } if (type.IsValueType) { System.Reflection.Emit.LocalBuilder local = ilg.DeclareLocal(type, type.Name + "Value"); ilg.Ldloca(local); ilg.InitObj(type); ilg.Ldloc(local); } else { // Special case XElement // codegen the same as 'internal XElement : this("default") { }' ConstructorInfo ctor = GetConstructor() !; if (!ctor.IsPublic && type.FullName == "System.Xml.Linq.XElement") { Type?xName = type.Assembly.GetType("System.Xml.Linq.XName"); if (xName != null) { MethodInfo?XName_op_Implicit = xName.GetMethod( "op_Implicit", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public, new Type[] { typeof(string) } ); ConstructorInfo?XElement_ctor = type.GetConstructor( BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, new Type[] { xName } ); if (XName_op_Implicit != null && XElement_ctor != null) { ilg.Ldstr("default"); ilg.Call(XName_op_Implicit); ctor = XElement_ctor; } } } ilg.New(ctor); } ilg.ConvertValue(this.UnderlyingType, Globals.TypeOfIXmlSerializable); ilg.Ret(); return((CreateXmlSerializableDelegate)ilg.EndMethod()); }
static void EmitCatList(ILGenerator gen, LocalBuilder local) { gen.Emit(OpCodes.Ldloc, local); gen.Emit(OpCodes.Call, GetFuncMethodInfo((List <object> list) => new Cat(list))); }
public virtual void Emit(System.Reflection.Emit.OpCode opcode, System.Reflection.Emit.LocalBuilder local) { }
static void EmitApplyAnonymousTemplate(ILGenerator gen, StringTemplate anonymous, LocalBuilder attributes) { // Stack behavior: ... => ..., (StringTemplate)result int index; lock ( _anonymousTemplates ) { index = _anonymousTemplates.Count; _anonymousTemplates.Add(anonymous); } EmitLoadChunk(gen); EmitLoadSelf(gen); gen.Emit(OpCodes.Ldloc, attributes); gen.Emit(OpCodes.Ldc_I4, index); gen.Emit(OpCodes.Call, GetFuncMethodInfo(( int i ) => _anonymousTemplates[i])); gen.Emit(OpCodes.Call, GetFuncMethodInfo( (ASTExpr chunk, StringTemplate self, List <object> attr, StringTemplate anon) => chunk.ApplyTemplateToListOfAttributes(self, attr, anon))); }
/// <summary>Puts the specified instruction onto the Microsoft intermediate language (MSIL) stream followed by the index of the given local variable.</summary> /// <param name="opcode">The MSIL instruction to be emitted onto the stream. </param> /// <param name="local">A local variable. </param> /// <exception cref="T:System.ArgumentException">The parent method of the <paramref name="local" /> parameter does not match the method associated with this <see cref="T:System.Reflection.Emit.XsILGenerator" />. </exception> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="local" /> is null. </exception> /// <exception cref="T:System.InvalidOperationException"> /// <paramref name="opcode" /> is a single-byte instruction, and <paramref name="local" /> represents a local variable with an index greater than Byte.MaxValue. </exception> /// <param name="generator">The <see cref="T:System.Reflection.Emit.XsILGenerator" /> to emit instructions from</param> public static XsILGenerator FluentEmit(this XsILGenerator generator, System.Reflection.Emit.OpCode opcode, System.Reflection.Emit.LocalBuilder local) { generator.Emit(opcode, local); return(generator); }
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); }
public abstract void EmitWriteLine(System.Reflection.Emit.LocalBuilder localBuilder);
public abstract void Emit(System.Reflection.Emit.OpCode opcode, System.Reflection.Emit.LocalBuilder local);