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 SetupException(IMethodBuilder Method, Utilities.Reflection.Emit.Commands.Catch Catch, IPropertyBuilder AspectusException) { VariableBase ExceptionArgs = Method.NewObj(typeof(Utilities.Reflection.AOP.EventArgs.Exception).GetConstructor(new Type[0])); ExceptionArgs.Call(typeof(Utilities.Reflection.AOP.EventArgs.Exception).GetProperty("InternalException").GetSetMethod(), new object[] { Catch.Exception }); 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(Utilities.Reflection.AOP.EventArgs.Exception) }); Method.Call(null, TempMethod, new object[] { AspectusException, IEventsThis, ExceptionArgs }); }
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(); } }