Пример #1
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);
        }
Пример #2
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();
            }
        }
        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();
            }
        }