/// <summary> /// Provides the implementation for operations that set member values. /// To specify dynamic behavior for operations such as setting a value for a property. /// </summary> /// <param name="binder">Provides information about the object that called the dynamic operation.</param> /// <param name="value">The value to set to the member.</param> /// <returns>true if the operation is successful; otherwise, false. </returns> public override bool TrySetMember(SetMemberBinder binder, object value) { var property = GetProperty(binder); if (property != null) { var oldValue = property.GetValue(source, null); if (oldValue != value) { WeavingContext context = new WeavingContext(source, binder, returnValue: value); try { chain.PropertyChanging(context); property.SetValue(source, value, null); chain.PropertyChanged(context); } catch (Exception ex) { ProcessException(context, ex); } } return(true); } return(false); }
private void ProcessException(WeavingContext context, Exception ex) { var exp = new DynamciException("DynamicAspectError", ex); if (chain.ExceptionMethodCall(context, exp) == false) { throw exp; } }
internal void PropertyChanged(WeavingContext context) { if (Last != null) { var current = Last; while (current != null) { current.Value.OnPropertyChanged(context); current = current.Previous; } } }
internal void AfterMethodCall(WeavingContext context) { if (Last != null) { var current = Last; while (current != null) { current.Value.OnAfterMethodCall(context); current = current.Previous; } } }
internal void BeforeGetValue(WeavingContext context) { if (First == null) { return; } var current = First; while (current != null) { current.Value.OnBeforeGetValue(context); current = current.Next; } ; }
internal void AroundMethodcall(WeavingContext context) { if (First == null) { return; } var current = First; while (current != null) { current.Value.OnAroundMethodCall(context); current = current.Next; } ; }
internal void PropertyChanging(WeavingContext context) { if (First == null) { return; } var current = First; while (current != null) { current.Value.OnPropertyChanging(context); current = current.Next; } ; }
internal bool ExceptionMethodCall(WeavingContext context, Exception ex) { if (First == null) { return(false); } var current = First; while (current != null) { if (current.Value.OnExceptionMethodCall(context, ex)) { return(true); } current = current.Next; } ; return(false); }
/// <summary> /// Overrides base method to provide a method template for weave methods that implements <see cref="IAspect"/> interface. /// </summary> /// <param name="binder"> Provides information about the dynamic operation. The binder.Name property /// provides the name of the member on which the dynamic operation is performed. <see cref="DynamicObject.TryInvokeMember"/></param> /// <param name="args"> The arguments that are passed to the object member during the invoke operation.</param> /// <param name="result">The result of the member invocation.</param> /// <returns> true if the operation is successful; otherwise, false. </returns> public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) { result = null; var mi = typeof(T).GetMethods().FirstOrDefault(m => m.Name == binder.Name && binder.ReturnType.IsAssignableFrom(m.ReturnType) && IsMatchParameters(m.GetParameters(), args)); if (mi != null) { WeavingContext context = new WeavingContext(source, binder, args); try { chain.BeforeMethodCall(context); if (chain.HasAroundMethod(context)) { chain.AroundMethodcall(context); } else { context.ReturnValue = mi.Invoke(source, args); } } catch (Exception ex) { ProcessException(context, ex); } finally { chain.AfterMethodCall(context); result = context.ReturnValue; } return(true); } else { return(false); } }
/// <summary> /// Provides the implementation for operations that get member values. /// Specifying dynamic behavior for operations such as getting a value for a property. /// </summary> /// <param name="binder">Provides information about the object that called the dynamic operation. /// The binder.Name property provides the name of the member on which the dynamic /// operation is performed. /// </param> /// <param name="value">The result of the get operation.</param> /// <returns> true if the operation is successful; otherwise, false.</returns> public override bool TryGetMember(GetMemberBinder binder, out object result) { result = null; var property = GetProperty(binder); if (property != null) { WeavingContext context = new WeavingContext(source, binder, returnValue: result); try { chain.BeforeGetValue(context); result = property.GetValue(source, null); chain.AfterGetValue(context); } catch (Exception ex) { ProcessException(context, ex); } return(true); } return(false); }
internal bool HasAroundMethod(WeavingContext context) { return(this.Any(a => a.HasArroundMethod(context))); }
/// <summary> /// Default implmentation of <see cref="IAspect"/>'s OnHasArroundMethod method. /// </summary> /// <param name="context">The parameter contains call site info, as well as meta binder info.<see cref="WeavingContext" /></param> public virtual bool HasArroundMethod(WeavingContext context) { return(false); }
/// <summary> /// Default implmentation of <see cref="IAspect"/>'s OnAroundMethodCall method. /// </summary> /// <param name="context">The parameter contains call site info, as well as meta binder info.<see cref="WeavingContext" /></param> public virtual void OnAroundMethodCall(WeavingContext context) { }
/// <summary> /// Default implmentation of <see cref="IAspect"/>'s OnAfterMethodCall method. /// </summary> /// <param name="context">The parameter contains call site info, as well as meta binder info.<see cref="WeavingContext" /></param> public virtual void OnAfterMethodCall(WeavingContext context) { }
/// <summary> /// Default implmentation of <see cref="IAspect"/>'s OnBeforeMethodCall method. /// </summary> /// <param name="context">The parameter contains call site info, as well as meta binder info.<see cref="WeavingContext" /></param> public virtual bool OnExceptionMethodCall(WeavingContext context, Exception ex) { return(false); }
/// <summary> /// Implmentation of <see cref="IAspect"/>'s OnBeforeGetValue method. /// </summary> /// <param name="context">The parameter contains call site info, as well as meta binder info.<see cref="WeavingContext" /></param> public virtual void OnBeforeGetValue(WeavingContext context) { }
/// <summary> /// Implmentation of <see cref="IAspect"/>'s OnAfterGetValue method. /// </summary> /// <param name="context">The parameter contains call site info, as well as meta binder info.<see cref="WeavingContext" /></param> public virtual void OnAfterGetValue(WeavingContext context) { }
/// <summary> /// Default implmentation of <see cref="IAspect"/>'s OnPropertyChanging method. /// </summary> /// <param name="context">The parameter contains call site info, as well as meta binder info.<see cref="WeavingContext" /></param> public virtual void OnPropertyChanging(WeavingContext context) { }
/// <summary> /// Default implmentation of <see cref="IAspect"/>'s OnBeforeMethodCall method. /// </summary> /// <param name="context">The parameter contains call site info, as well as meta binder info.<see cref="WeavingContext" /></param> public virtual bool OnExceptionMethodCall(WeavingContext context, Exception ex) { return false; }
/// <summary> /// Default implmentation of <see cref="IAspect"/>'s OnBeforeMethodCall method. /// </summary> /// <param name="context">The parameter contains call site info, as well as meta binder info.<see cref="WeavingContext" /></param> public virtual void OnBeforeMethodCall(WeavingContext context) { }
/// <summary> /// Default implmentation of <see cref="IAspect"/>'s OnHasArroundMethod method. /// </summary> /// <param name="context">The parameter contains call site info, as well as meta binder info.<see cref="WeavingContext" /></param> public virtual bool HasArroundMethod(WeavingContext context) { return false; }