예제 #1
0
 /// <summary>
 /// Creates a new label instance.
 /// </summary>
 /// <param name="label"> The underlying label. </param>
 public ReflectionEmitILLabel(System.Reflection.Emit.Label label)
 {
     if (label == null)
     {
         throw new ArgumentNullException(nameof(label));
     }
     this.UnderlyingLabel = label;
 }
        /// <summary>
        /// Creates a jump table.  A value is popped from the stack - this value indicates the
        /// index of the label in the <paramref name="labels"/> array to jump to.
        /// </summary>
        /// <param name="labels"> A array of labels. </param>
        public override void Switch(ILLabel[] labels)
        {
            if (labels == null)
            {
                throw new ArgumentNullException(nameof(labels));
            }

            var reflectionLabels = new System.Reflection.Emit.Label[labels.Length];

            for (int i = 0; i < labels.Length; i++)
            {
                reflectionLabels[i] = ((ReflectionEmitILLabel)labels[i]).UnderlyingLabel;
            }
            this.generator.Emit(OpCodes.Switch, reflectionLabels);
        }
        private void SetupMethod(Type BaseType, IMethodBuilder Method, IPropertyBuilder AspectusStarting,
                                 IPropertyBuilder AspectusEnding, IPropertyBuilder AspectusException, MethodInfo BaseMethod)
        {
            if (BaseMethod == null)
            {
                BaseMethod = BaseType.GetMethod(Method.Name);
            }
            Method.SetCurrentMethod();
            System.Reflection.Emit.Label EndLabel = Method.Generator.DefineLabel();
            VariableBase ReturnValue = Method.ReturnType != typeof(void) ? Method.CreateLocal("FinalReturnValue", Method.ReturnType) : null;

            Utilities.Reflection.Emit.Commands.Try Try = Method.Try();
            {
                SetupStart(Method, EndLabel, ReturnValue, AspectusStarting);
                Aspects.ForEach(x => x.SetupStartMethod(Method, BaseType));
                List <ParameterBuilder> Parameters = new List <ParameterBuilder>();
                Method.Parameters.For(1, Method.Parameters.Count - 1, x => Parameters.Add(x));
                if (Method.ReturnType != typeof(void) && BaseMethod != null)
                {
                    ReturnValue.Assign(Method.This.Call(BaseMethod, Parameters.ToArray()));
                }
                else if (BaseMethod != null)
                {
                    Method.This.Call(BaseMethod, Parameters.ToArray());
                }
                SetupEnd(Method, ReturnValue, AspectusEnding);
                Aspects.ForEach(x => x.SetupEndMethod(Method, BaseType, ReturnValue));
                Method.Generator.MarkLabel(EndLabel);
            }
            Utilities.Reflection.Emit.Commands.Catch Catch = Try.StartCatchBlock(typeof(System.Exception));
            {
                SetupException(Method, Catch, AspectusException);
                Aspects.ForEach(x => x.SetupExceptionMethod(Method, BaseType));
                Catch.Rethrow();
            }
            Try.EndTryBlock();

            if (Method.ReturnType != typeof(void))
            {
                Method.Return(ReturnValue);
            }
            else
            {
                Method.Return();
            }
        }
        private void SetupStart(IMethodBuilder Method, System.Reflection.Emit.Label EndLabel,
                                VariableBase ReturnValue, IPropertyBuilder AspectusStarting)
        {
            VariableBase StartingArgs = Method.NewObj(typeof(Starting).GetConstructor(new Type[0]));

            StartingArgs.Call(typeof(Starting).GetProperty("MethodName").GetSetMethod(), new object[] { Method.Name });

            VariableBase ParameterList = StartingArgs.Call(typeof(Starting).GetProperty("Parameters").GetGetMethod());

            for (int x = 1; x < Method.Parameters.Count; ++x)
            {
                if (Method.Parameters[x].DataType != null && Method.Parameters[x].DataType.IsValueType)
                {
                    ParameterList.Call(typeof(List <object>).GetMethod("Add"), new object[] { Method.Box(Method.Parameters[x]) });
                }
                else
                {
                    ParameterList.Call(typeof(List <object>).GetMethod("Add"), new object[] { Method.Parameters[x] });
                }
            }

            VariableBase IEventsThis     = Method.Cast(Method.This, typeof(IEvents));
            Type         EventHelperType = typeof(Utilities.Events.EventHelper);

            MethodInfo[] Methods = EventHelperType.GetMethods()
                                   .Where <MethodInfo>(x => x.GetParameters().Length == 3)
                                   .ToArray();
            MethodInfo TempMethod = Methods.Length > 0 ? Methods[0] : null;

            TempMethod = TempMethod.MakeGenericMethod(new Type[] { typeof(Starting) });
            Method.Call(null, TempMethod, new object[] { AspectusStarting, IEventsThis, StartingArgs });
            if (Method.ReturnType != typeof(void))
            {
                VariableBase TempReturnValue             = StartingArgs.Call(typeof(Starting).GetProperty("ReturnValue").GetGetMethod());
                VariableBase TempNull                    = Method.CreateLocal("TempNull", typeof(object));
                Utilities.Reflection.Emit.Commands.If If = Method.If(TempReturnValue, Utilities.Reflection.Emit.Enums.Comparison.NotEqual, TempNull);
                {
                    ReturnValue.Assign(TempReturnValue);
                    Method.Generator.Emit(System.Reflection.Emit.OpCodes.Br, EndLabel);
                }
                Method.SetCurrentMethod();
                If.EndIf();
            }
        }
 public bool Equals(System.Reflection.Emit.Label obj)
 {
     return(default(bool));
 }
 public virtual void MarkLabel(System.Reflection.Emit.Label loc)
 {
 }
 public virtual void Emit(System.Reflection.Emit.OpCode opcode, System.Reflection.Emit.Label label)
 {
 }
예제 #8
0
 public bool Equals(System.Reflection.Emit.Label obj)
 {
     throw null;
 }
예제 #9
0
 public virtual void MarkLabel(System.Reflection.Emit.Label loc) => throw null;
예제 #10
0
        private void AddNewTab()
        {
            TabName = TabNameBox.Text;

            TabItem newTab = new TabItem();
            Grid    gr     = new Grid();

            DataGrid dg1 = new DataGrid();

            dg1.VerticalAlignment   = VerticalAlignment.Top;
            dg1.HorizontalAlignment = HorizontalAlignment.Left;
            dg1.Width  = 300;
            dg1.Height = 200;
            //dg1.RenderTransformOrigin = new System.Windows.Point(10, 10);

            DataTable   tb  = new DataTable();
            DataGridRow dgr = new DataGridRow();

            tb.Columns.Add();
            tb.Rows.Add();
            dg1.ItemsSource = tb.AsDataView();

            gr.Children.Add(dg1);

            Button btn = new Button();

            btn.Content             = "New Button";
            btn.Width               = 100;
            btn.Height              = 30;
            btn.Margin              = new Thickness(10, 230, 10, 10);
            btn.VerticalAlignment   = VerticalAlignment.Top;
            btn.HorizontalAlignment = HorizontalAlignment.Left;
            btn.Click              += (sender, args) =>
            {
                BtnOnClickEvent(btn, args, newTab, (TextBox)gr.Children[2]);
            };

            TextBox TxtBox = new TextBox();

            TxtBox.Name = "Text_Box";
            TxtBox.VerticalAlignment   = VerticalAlignment.Top;
            TxtBox.HorizontalAlignment = HorizontalAlignment.Left;
            TxtBox.Margin = new Thickness(10, 280, 10, 10);
            TxtBox.Width  = 300;

            gr.Children.Add(TxtBox);

            gr.Children.Add(btn);

            gr.VerticalAlignment   = VerticalAlignment.Top;
            gr.HorizontalAlignment = HorizontalAlignment.Left;

            newTab.Content = gr;
            newTab.Header  = TabName;

            Label project_or_task_lbl = new Label();



            Tab_control_test.Items.Add(newTab);
        }
        /// <summary>
        /// Creates a jump table.  A value is popped from the stack - this value indicates the
        /// index of the label in the <paramref name="labels"/> array to jump to.
        /// </summary>
        /// <param name="labels"> A array of labels. </param>
        public override void Switch(ILLabel[] labels)
        {
            if (labels == null)
                throw new ArgumentNullException("labels");

            var reflectionLabels = new System.Reflection.Emit.Label[labels.Length];
            for (int i = 0; i < labels.Length; i++)
                reflectionLabels[i] = ((ReflectionEmitILLabel)labels[i]).UnderlyingLabel;
            this.generator.Emit(OpCodes.Switch, reflectionLabels);
        }
 /// <summary>Puts the specified instruction onto the Microsoft intermediate language (MSIL) stream and leaves space to include a label when fixes are done.</summary>
 /// <param name="opcode">The MSIL instruction to be emitted onto the stream. </param>
 /// <param name="label">The label to which to branch from this location. </param>
 /// <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.Label label)
 {
     generator.Emit(opcode, label);
     return(generator);
 }
예제 #13
0
        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);
        }
예제 #14
0
 public abstract void MarkLabel(System.Reflection.Emit.Label loc);
예제 #15
0
 public abstract void Emit(System.Reflection.Emit.OpCode opcode, System.Reflection.Emit.Label label);