Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
        private void ProcessException(WeavingContext context, Exception ex)
        {
            var exp = new DynamciException("DynamicAspectError", ex);

            if (chain.ExceptionMethodCall(context, exp) == false)
            {
                throw exp;
            }
        }
Beispiel #3
0
 internal void PropertyChanged(WeavingContext context)
 {
     if (Last != null)
     {
         var current = Last;
         while (current != null)
         {
             current.Value.OnPropertyChanged(context);
             current = current.Previous;
         }
     }
 }
Beispiel #4
0
 internal void AfterMethodCall(WeavingContext context)
 {
     if (Last != null)
     {
         var current = Last;
         while (current != null)
         {
             current.Value.OnAfterMethodCall(context);
             current = current.Previous;
         }
     }
 }
Beispiel #5
0
        internal void BeforeGetValue(WeavingContext context)
        {
            if (First == null)
            {
                return;
            }

            var current = First;

            while (current != null)
            {
                current.Value.OnBeforeGetValue(context);
                current = current.Next;
            }
            ;
        }
Beispiel #6
0
        internal void AroundMethodcall(WeavingContext context)
        {
            if (First == null)
            {
                return;
            }

            var current = First;

            while (current != null)
            {
                current.Value.OnAroundMethodCall(context);
                current = current.Next;
            }
            ;
        }
Beispiel #7
0
        internal void PropertyChanging(WeavingContext context)
        {
            if (First == null)
            {
                return;
            }

            var current = First;

            while (current != null)
            {
                current.Value.OnPropertyChanging(context);
                current = current.Next;
            }
            ;
        }
Beispiel #8
0
        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);
        }
Beispiel #9
0
        /// <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);
            }
        }
Beispiel #10
0
        /// <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);
        }
Beispiel #11
0
 internal bool HasAroundMethod(WeavingContext context)
 {
     return(this.Any(a => a.HasArroundMethod(context)));
 }
Beispiel #12
0
 /// <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);
 }
Beispiel #13
0
 /// <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)
 {
 }
Beispiel #14
0
 /// <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)
 {
 }
Beispiel #15
0
 /// <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);
 }
Beispiel #16
0
 /// <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)
 {
 }
Beispiel #17
0
 /// <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)
 {
 }
Beispiel #18
0
 /// <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)
 {
 }
Beispiel #19
0
 /// <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)
 {
 }
Beispiel #20
0
 /// <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)
 {
 }
Beispiel #21
0
 /// <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;
 }
Beispiel #22
0
 /// <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)
 {
 }
Beispiel #23
0
 /// <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)
 {
 }
Beispiel #24
0
 /// <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)
 {
 }
Beispiel #25
0
 /// <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)
 {
 }
Beispiel #26
0
 /// <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;
 }
Beispiel #27
0
 /// <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)
 {
 }