static public Advisor Returning(this Advisor.IAfter @this, Action <ILGenerator> advise) { return(@this.Returning(new Action <MethodBuilder>(_Method => { var _body = _Method.GetILGenerator(); advise(_body); _body.Emit(OpCodes.Ret); }))); }
static private Advisor Throwing(this Advisor.IAfter @this, Action <MethodBuilder> advise) { var _signature = @this.Generator.Method.GetParameters().Select(_Parameter => _Parameter.ParameterType).ToArray(); if (@this.Generator.Method.ReturnType() == Metadata.Void) { return(@this.Generator.Create((_Type, _Instance, _Arguments) => { var _advice = _Type.DefineMethod("<Advice>", MethodAttributes.Static | MethodAttributes.Private, CallingConventions.Standard, Metadata.Void, @this.Generator.Method.IsStatic ? _signature : new Type[] { @this.Generator.Method.DeclaringType }.Concat(_signature).Concat(new Type[] { Metadata <Exception> .Type }).ToArray()); advise(_advice); var _method = _Type.DefineMethod("IAdvice.Throw", MethodAttributes.Private | MethodAttributes.Virtual, CallingConventions.HasThis, Metadata.Void, new Type[] { Metadata <Exception> .Type.MakeByRefType() }); var _body = _method.GetILGenerator(); if (_Instance != null) { _body.Emit(OpCodes.Ldarg_0); _body.Emit(OpCodes.Ldfld, _Instance); } foreach (var _argument in _Arguments) { _body.Emit(OpCodes.Ldarg_0); _body.Emit(OpCodes.Ldfld, _argument); } _body.Emit(OpCodes.Ldarg_1); _body.Emit(OpCodes.Call, _advice); _body.Emit(OpCodes.Ret); _Type.DefineMethodOverride(_method, Metadata <IAdvice> .Method(_IAdvice => _IAdvice.Throw(ref Metadata <Exception> .Value))); })); } else { return(@this.Generator.Create((_Type, _Instance, _Arguments) => { var _advice = _Type.DefineMethod("<Advice>", MethodAttributes.Static | MethodAttributes.Private, CallingConventions.Standard, Metadata.Void, @this.Generator.Method.IsStatic ? _signature : new Type[] { @this.Generator.Method.DeclaringType }.Concat(_signature).Concat(new Type[] { Metadata <Exception> .Type }).ToArray()); advise(_advice); var _method = _Type.DefineMethod("IAdvice.Throw", MethodAttributes.Private | MethodAttributes.Virtual, CallingConventions.HasThis); _method.SetReturnType(Metadata.Void); _method.SetParameters(new Type[] { Metadata <Exception> .Type.MakeByRefType(), _method.DefineGenericParameters("T")[0].MakeByRefType() }); var _body = _method.GetILGenerator(); if (_Instance != null) { _body.Emit(OpCodes.Ldarg_0); _body.Emit(OpCodes.Ldfld, _Instance); } foreach (var _argument in _Arguments) { _body.Emit(OpCodes.Ldarg_0); _body.Emit(OpCodes.Ldfld, _argument); } _body.Emit(OpCodes.Ldarg_1); _body.Emit(OpCodes.Call, _advice); _body.Emit(OpCodes.Ret); _Type.DefineMethodOverride(_method, Metadata <IAdvice> .Method(_IAdvice => _IAdvice.Throw(ref Metadata <Exception> .Value, ref Metadata <object> .Value)).GetGenericMethodDefinition()); })); } }
static public Advisor Returning(this Advisor.IAfter @this, Func <Advisor.Execution.Returning, Expression> advise) { var _signature = @this.Generator.Method.GetParameters().Select(_Parameter => Expression.Parameter(_Parameter.ParameterType)); return(@this.Returning(new Action <MethodBuilder>(_Method => { if (@this.Generator.Method.IsStatic) { Expression.Lambda(advise(new Advisor.Execution.Returning(@this.Generator.Method, null, new Collection <Expression>(_signature))), _signature).CompileToMethod(_Method); } else { var _instance = Expression.Parameter(@this.Generator.Method.DeclaringType); Expression.Lambda(advise(new Advisor.Execution.Returning(@this.Generator.Method, _instance, new Collection <Expression>(_signature))), new ParameterExpression[] { _instance }.Concat(_signature)).CompileToMethod(_Method); } }))); }
static public Advisor Returning(this Advisor.IAfter @this, Action advice) { return(@this.Returning(Expression.Call(Expression.Field(null, Aspect.Module.DefineField(advice)), Metadata <Action> .Method(_Action => _Action.Invoke())))); }
static public Advisor Returning(this Advisor.IAfter @this, Expression advice) { return(@this.Returning(_Execution => advice)); }
static public Advisor.After.IReturning Returning(this Advisor.IAfter @this) { return(new Advisor.After.Returning(@this)); }
internal Returning(Advisor.IAfter after) { this.m_After = after; }