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