Пример #1
0
        public void Cast()
        {
            Utilities.Reflection.Emit.Assembly    Assembly = new Utilities.Reflection.Emit.Assembly("TestAssembly");
            Utilities.Reflection.Emit.TypeBuilder TestType = Assembly.CreateType("TestType");
            IMethodBuilder Method = TestType.CreateMethod("TestMethod");
            VariableBase   Local1 = Method.CreateLocal("Local1", typeof(int));
            VariableBase   Local2 = Method.CreateLocal("Local2", typeof(int));

            Assert.DoesNotThrow(() => Method.Cast(Method.This, typeof(object)));
        }
Пример #2
0
        public void Box()
        {
            Utilities.Reflection.Emit.Assembly    Assembly = new Utilities.Reflection.Emit.Assembly("TestAssembly");
            Utilities.Reflection.Emit.TypeBuilder TestType = Assembly.CreateType("TestType");
            IMethodBuilder Method = TestType.CreateMethod("TestMethod");
            VariableBase   Local1 = Method.CreateLocal("Local1", typeof(int));
            VariableBase   Local2 = Method.CreateLocal("Local2", typeof(int));

            Assert.NotNull(Method.Box(Local1));
        }
        public void Assign()
        {
            Utilities.Reflection.Emit.Assembly    Assembly = new Utilities.Reflection.Emit.Assembly("TestAssembly");
            Utilities.Reflection.Emit.TypeBuilder TestType = Assembly.CreateType("TestType");
            IMethodBuilder Method = TestType.CreateMethod("TestMethod");
            VariableBase   Local1 = Method.CreateLocal("Local1", typeof(int));
            VariableBase   Local2 = Method.CreateLocal("Local2", typeof(int));

            Assert.DoesNotThrow <Exception>(() => Method.Assign(Local1, Local2));
        }
Пример #4
0
        public void Throw()
        {
            Utilities.Reflection.Emit.Assembly    Assembly = new Utilities.Reflection.Emit.Assembly("TestAssembly");
            Utilities.Reflection.Emit.TypeBuilder TestType = Assembly.CreateType("TestType");
            IMethodBuilder Method = TestType.CreateMethod("TestMethod");
            VariableBase   Local1 = Method.CreateLocal("Local1", typeof(int));
            VariableBase   Local2 = Method.CreateLocal("Local2", typeof(int));

            Method.Throw(Method.NewObj(typeof(Exception)));
        }
Пример #5
0
        public void Return()
        {
            Utilities.Reflection.Emit.Assembly    Assembly = new Utilities.Reflection.Emit.Assembly("TestAssembly");
            Utilities.Reflection.Emit.TypeBuilder TestType = Assembly.CreateType("TestType");
            IMethodBuilder Method = TestType.CreateMethod("TestMethod", ReturnType: typeof(int));
            VariableBase   Local1 = Method.CreateLocal("Local1", typeof(int));
            VariableBase   Local2 = Method.CreateLocal("Local2", typeof(int));

            Method.Return(Local1);
        }
Пример #6
0
        public void NewObj()
        {
            Utilities.Reflection.Emit.Assembly    Assembly = new Utilities.Reflection.Emit.Assembly("TestAssembly");
            Utilities.Reflection.Emit.TypeBuilder TestType = Assembly.CreateType("TestType");
            IMethodBuilder Method = TestType.CreateMethod("TestMethod");
            VariableBase   Local1 = Method.CreateLocal("Local1", typeof(int));
            VariableBase   Local2 = Method.CreateLocal("Local2", typeof(int));

            Assert.NotNull(Method.NewObj(typeof(DateTime), new object[] { Local1 }));
        }
Пример #7
0
        public void Call()
        {
            Utilities.Reflection.Emit.Assembly    Assembly = new Utilities.Reflection.Emit.Assembly("TestAssembly");
            Utilities.Reflection.Emit.TypeBuilder TestType = Assembly.CreateType("TestType");
            IMethodBuilder Method  = TestType.CreateMethod("TestMethod");
            IMethodBuilder Method2 = TestType.CreateMethod("TestMethod2");
            VariableBase   Local1  = Method.CreateLocal("Local1", typeof(int));
            VariableBase   Local2  = Method.CreateLocal("Local2", typeof(int));

            Method.This.Call(Method2, null);
        }
Пример #8
0
        public void While()
        {
            Utilities.Reflection.Emit.Assembly    Assembly = new Utilities.Reflection.Emit.Assembly("TestAssembly");
            Utilities.Reflection.Emit.TypeBuilder TestType = Assembly.CreateType("TestType");
            IMethodBuilder Method = TestType.CreateMethod("TestMethod");
            VariableBase   Local1 = Method.CreateLocal("Local1", typeof(int));
            VariableBase   Local2 = Method.CreateLocal("Local2", typeof(int));

            Utilities.Reflection.Emit.Commands.While While = Method.While(Local1, Utilities.Reflection.Emit.Enums.Comparison.Equal, Local2);
            Method.EndWhile(While);
            Assert.NotNull(While);
        }
Пример #9
0
        public void TryCatch()
        {
            Utilities.Reflection.Emit.Assembly    Assembly = new Utilities.Reflection.Emit.Assembly("TestAssembly");
            Utilities.Reflection.Emit.TypeBuilder TestType = Assembly.CreateType("TestType");
            IMethodBuilder Method = TestType.CreateMethod("TestMethod");
            VariableBase   Local1 = Method.CreateLocal("Local1", typeof(int));
            VariableBase   Local2 = Method.CreateLocal("Local2", typeof(int));

            Utilities.Reflection.Emit.Commands.Try   Try   = Method.Try();
            Utilities.Reflection.Emit.Commands.Catch Catch = Method.Catch(typeof(Exception));
            Method.EndTry();
            Assert.NotNull(Try);
            Assert.NotNull(Catch);
        }
Пример #10
0
 /// <summary>
 ///     Constructor
 /// </summary>
 /// <param name="objectCallingOn">Object calling on</param>
 /// <param name="method">Method builder</param>
 /// <param name="methodCalling">Method calling on the object</param>
 /// <param name="parameters">List of parameters to send in</param>
 public Call(IMethodBuilder method, VariableBase objectCallingOn, MethodInfo methodCalling, object[] parameters)
 {
     ObjectCallingOn = objectCallingOn;
     MethodCalling = methodCalling;
     MethodCallingFrom = method;
     if (methodCalling.ReturnType != null && methodCalling.ReturnType != typeof (void))
     {
         Result =
             method.CreateLocal(
                 methodCalling.Name + "ReturnObject" +
                 MethodBase.ObjectCounter.ToString(CultureInfo.InvariantCulture), methodCalling.ReturnType);
     }
     if (parameters != null)
     {
         Parameters = new VariableBase[parameters.Length];
         for (int x = 0; x < parameters.Length; ++x)
         {
             if (parameters[x] is VariableBase)
                 Parameters[x] = (VariableBase) parameters[x];
             else
                 Parameters[x] = MethodCallingFrom.CreateConstant(parameters[x]);
         }
     }
     else
     {
         Parameters = null;
     }
 }
Пример #11
0
 /// <summary>
 ///     Constructor
 /// </summary>
 /// <param name="objectCallingOn">Object calling on</param>
 /// <param name="method">Method builder</param>
 /// <param name="methodCalling">Method calling on the object</param>
 /// <param name="parameters">List of parameters to send in</param>
 public Call(IMethodBuilder method, VariableBase objectCallingOn, MethodInfo methodCalling, object[] parameters)
 {
     ObjectCallingOn   = objectCallingOn;
     MethodCalling     = methodCalling;
     MethodCallingFrom = method;
     if (methodCalling.ReturnType != null && methodCalling.ReturnType != typeof(void))
     {
         Result =
             method.CreateLocal(
                 methodCalling.Name + "ReturnObject" +
                 MethodBase.ObjectCounter.ToString(CultureInfo.InvariantCulture), methodCalling.ReturnType);
     }
     if (parameters != null)
     {
         Parameters = new VariableBase[parameters.Length];
         for (int x = 0; x < parameters.Length; ++x)
         {
             if (parameters[x] is VariableBase)
             {
                 Parameters[x] = (VariableBase)parameters[x];
             }
             else
             {
                 Parameters[x] = MethodCallingFrom.CreateConstant(parameters[x]);
             }
         }
     }
     else
     {
         Parameters = null;
     }
 }
Пример #12
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="ObjectCallingOn">Object calling on</param>
 /// <param name="Method">Method builder</param>
 /// <param name="MethodCalling">Method calling on the object</param>
 /// <param name="Parameters">List of parameters to send in</param>
 public Call(IMethodBuilder Method, VariableBase ObjectCallingOn, MethodInfo MethodCalling, object[] Parameters)
     : base()
 {
     this.ObjectCallingOn = ObjectCallingOn;
     this.MethodCalling = MethodCalling;
     this.MethodCallingFrom = Method;
     if (MethodCalling.ReturnType != null && MethodCalling.ReturnType != typeof(void))
     {
         Result = Method.CreateLocal(MethodCalling.Name + "ReturnObject"+YunShop.Utilities.Reflection.Emit.BaseClasses.MethodBase.ObjectCounter.ToString(), MethodCalling.ReturnType);
     }
     if (Parameters != null)
     {
         this.Parameters = new VariableBase[Parameters.Length];
         for (int x = 0; x < Parameters.Length; ++x)
         {
             if (Parameters[x] is VariableBase)
                 this.Parameters[x] = (VariableBase)Parameters[x];
             else
                 this.Parameters[x] = MethodCallingFrom.CreateConstant(Parameters[x]);
         }
     }
     else
     {
         this.Parameters = null;
     }
 }
Пример #13
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="ObjectCallingOn">Object calling on</param>
 /// <param name="Method">Method builder</param>
 /// <param name="MethodCalling">Method calling on the object</param>
 /// <param name="Parameters">List of parameters to send in</param>
 public Call(IMethodBuilder Method, VariableBase ObjectCallingOn, MethodInfo MethodCalling, object[] Parameters)
     : base()
 {
     this.ObjectCallingOn   = ObjectCallingOn;
     this.MethodCalling     = MethodCalling;
     this.MethodCallingFrom = Method;
     if (MethodCalling.ReturnType != null && MethodCalling.ReturnType != typeof(void))
     {
         Result = Method.CreateLocal(MethodCalling.Name + "ReturnObject" + Utilities.Reflection.Emit.BaseClasses.MethodBase.ObjectCounter.ToString(CultureInfo.InvariantCulture), MethodCalling.ReturnType);
     }
     if (Parameters != null)
     {
         this.Parameters = new VariableBase[Parameters.Length];
         for (int x = 0; x < Parameters.Length; ++x)
         {
             if (Parameters[x] is VariableBase)
             {
                 this.Parameters[x] = (VariableBase)Parameters[x];
             }
             else
             {
                 this.Parameters[x] = MethodCallingFrom.CreateConstant(Parameters[x]);
             }
         }
     }
     else
     {
         this.Parameters = null;
     }
 }
Пример #14
0
        public void Save()
        {
            Utilities.Reflection.Emit.Assembly    Assembly = new Utilities.Reflection.Emit.Assembly("TestAssembly");
            Utilities.Reflection.Emit.TypeBuilder TestType = Assembly.CreateType("TestType");
            IMethodBuilder Method = TestType.CreateMethod("TestMethod");
            VariableBase   Local1 = Method.CreateLocal("Local1", typeof(int));

            Local1.Save(Method.Generator);
        }
        public void Load()
        {
            Utilities.Reflection.Emit.Assembly    Assembly = new Utilities.Reflection.Emit.Assembly("TestAssembly");
            Utilities.Reflection.Emit.TypeBuilder TestType = Assembly.CreateType("TestType");
            IMethodBuilder Method = TestType.CreateMethod("TestMethod");
            VariableBase   Local1 = Method.CreateLocal("Local1", typeof(int));

            Assert.DoesNotThrow(() => Local1.Load(Method.Generator));
        }
Пример #16
0
        public void Create()
        {
            Utilities.Reflection.Emit.Assembly    Assembly = new Utilities.Reflection.Emit.Assembly("TestAssembly");
            Utilities.Reflection.Emit.TypeBuilder TestType = Assembly.CreateType("TestType");
            IMethodBuilder Method = TestType.CreateMethod("TestMethod");
            VariableBase   Local1 = Method.CreateLocal("Local1", typeof(int));

            Assert.NotNull(Local1);
            Assert.Equal(typeof(int), Local1.DataType);
            Assert.Equal("Local1", Local1.Name);
        }
Пример #17
0
        private static void SetupEnd(IMethodBuilder method, VariableBase returnValue, IPropertyBuilder aspectusEnding)
        {
            VariableBase endingArgs = method.NewObj(typeof(Ending).GetConstructor(new Type[0]));

            endingArgs.Call(typeof(Ending).GetProperty("MethodName").GetSetMethod(), new object[] { method.Name });
            if (method.ReturnType != typeof(void) && returnValue.DataType != null && returnValue.DataType.IsValueType)
            {
                endingArgs.Call(typeof(Ending).GetProperty("ReturnValue").GetSetMethod(),
                                new object[] { method.Box(returnValue) });
            }
            else if (method.ReturnType != typeof(void))
            {
                endingArgs.Call(typeof(Ending).GetProperty("ReturnValue").GetSetMethod(), new object[] { returnValue });
            }
            VariableBase parameterList = endingArgs.Call(typeof(Ending).GetProperty("Parameters").GetGetMethod());

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

            VariableBase eventsThis      = method.Cast(method.This, typeof(IEvents));
            Type         eventHelperType = typeof(DelegateExtensions);

            MethodInfo[] methods = eventHelperType.GetMethods()
                                   .Where(x => x.GetParameters().Length == 3)
                                   .ToArray();
            MethodInfo tempMethod = methods.Length > 0 ? methods[0] : null;

            tempMethod = tempMethod.MakeGenericMethod(new[] { typeof(Ending) });
            method.Call(null, tempMethod, new object[] { aspectusEnding, eventsThis, endingArgs });
            if (method.ReturnType != typeof(void))
            {
                VariableBase tempReturnValue = endingArgs.Call(typeof(Ending).GetProperty("ReturnValue").GetGetMethod());
                VariableBase tempNull        = method.CreateLocal("TempNull", typeof(object));
                If           If = method.If(tempReturnValue, Comparison.NotEqual, tempNull);
                {
                    returnValue.Assign(tempReturnValue);
                }
                method.SetCurrentMethod();
                If.EndIf();
            }
        }
Пример #18
0
        private static void SetupMethod(Type baseType, IMethodBuilder method, IPropertyBuilder aspectusStarting,
                                        IPropertyBuilder aspectusEnding, IPropertyBuilder aspectusException,
                                        MethodInfo baseMethod)
        {
            if (baseMethod == null)
            {
                baseMethod = baseType.GetMethod(method.Name);
            }
            method.SetCurrentMethod();
            Label        endLabel    = method.Generator.DefineLabel();
            VariableBase returnValue = method.ReturnType != typeof(void)
                                           ? method.CreateLocal("FinalReturnValue", method.ReturnType)
                                           : null;
            Try Try = method.Try();
            {
                SetupStart(method, endLabel, returnValue, aspectusStarting);
                _aspects.ForEach(x => x.SetupStartMethod(method, baseType));
                var 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);
            }
            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();
            }
        }
Пример #19
0
        private void SetupEnd(IMethodBuilder Method, VariableBase ReturnValue, IPropertyBuilder AspectusEnding)
        {
            VariableBase EndingArgs = Method.NewObj(typeof(Ending).GetConstructor(new Type[0]));

            EndingArgs.Call(typeof(Ending).GetProperty("MethodName").GetSetMethod(), new object[] { Method.Name });
            if (Method.ReturnType != typeof(void) && ReturnValue.DataType != null && ReturnValue.DataType.IsValueType)
            {
                EndingArgs.Call(typeof(Ending).GetProperty("ReturnValue").GetSetMethod(), new object[] { Method.Box(ReturnValue) });
            }
            else if (Method.ReturnType != typeof(void))
            {
                EndingArgs.Call(typeof(Ending).GetProperty("ReturnValue").GetSetMethod(), new object[] { ReturnValue });
            }
            VariableBase ParameterList = EndingArgs.Call(typeof(Ending).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(Ending) });
            Method.Call(null, TempMethod, new object[] { AspectusEnding, IEventsThis, EndingArgs });
            if (Method.ReturnType != typeof(void))
            {
                VariableBase TempReturnValue             = EndingArgs.Call(typeof(Ending).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.SetCurrentMethod();
                If.EndIf();
            }
        }
Пример #20
0
        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 static 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.ElementAt(x).DataType != null && Method.Parameters.ElementAt(x).DataType.IsValueType)
                {
                    ParameterList.Call(typeof(List <object>).GetMethod("Add"), new object[] { Method.Box(Method.Parameters.ElementAt(x)) });
                }
                else
                {
                    ParameterList.Call(typeof(List <object>).GetMethod("Add"), new object[] { Method.Parameters.ElementAt(x) });
                }
            }

            VariableBase IEventsThis     = Method.Cast(Method.This, typeof(IEvents));
            Type         EventHelperType = typeof(Utilities.DataTypes.ExtensionMethods.DelegateExtensions);

            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();
            }
        }