Beispiel #1
0
 /// <summary>
 /// 拦截属性
 /// </summary>
 /// <param name="context">属性上下文</param>
 public void Advise(PropertyAdviceContext context)
 {
     if (context.IsGetter)
     {
         context.Proceed();
     }
     else
     {
         context.Proceed();
         if (context.Target is INotifyPropertyChangedEx dataContext)
         {
             dataContext.NotifyOfPropertyChange(context.TargetName);
         }
     }
 }
        void IPropertyAdvice.Advise(PropertyAdviceContext context)
        {
            var ctx = BeforeProcessing(new ActivationContext(context));

            if (ctx.ExecuteAlternate)
            {
                if (context.IsGetter && ctx.SkipTargetProcess)
                {
                    var task = AlternateProceed(ctx);
                    context.Value = task;
                }
                else
                {
                    AlternateProceed(ctx);
                }
            }
            if (!ctx.SkipTargetProcess)
            {
                try
                {
                    context.Proceed();
                }
                catch (Exception ex)
                {
                    HandleException(ctx, ex);
                    throw;
                }
            }
            ctx = AfterProcesssing(ctx);
            ctx.Clean();
        }
Beispiel #3
0
 public void Advise(PropertyAdviceContext context)
 {
     using (var t = TracerFactory.StartTracer(context.TargetType, string.Format("{0}_{1}", context.IsGetter ? "get" : "set", context.TargetProperty.Name)))
     {
         if (info.ContainsCharacters())
         {
             t.SetAdidtionalInformation(info);
         }
         try
         {
             context.Proceed();
         }
         catch (Exception ex)
         {
             if (TreatExceptionAsInformational)
             {
                 t.SetException(ex);
             }
             else
             {
                 t.SetErrorState(ex);
             }
             throw;
         }
     }
 }
        public void Advise(PropertyAdviceContext context)
        {
            if (context.HasValue)
            {
                if (context.Value != null && context.Value.ToString().ToLower() == "null")
                {
                    context.Value = null;
                }
            }

            context.Proceed();
        }
        public void Advise(PropertyAdviceContext propertyContext)
        {
            var classType    = propertyContext.TargetType;
            var propertyType = propertyContext.TargetProperty;

            if (!classType.IsSameOrSubclassOf(typeof(ObservableObject)))
            {
                throw new InvalidProgramException($"The Attribute {GetType().FullName} is not applicable to {classType.FullName}. {classType.FullName} must derived from {nameof(ObservableObject)}.");
            }

            // Getter
            if (propertyContext.IsGetter)
            {
                propertyContext.Proceed();
                return;
            }

            // Setter
            propertyContext.Proceed();
            (propertyContext.Target as ObservableObject).InvokePropertyChanged(propertyType.Name);
        }
        public void Advise(PropertyAdviceContext propertyContext)
        {
            var classInstance = propertyContext.Target;
            var classType     = propertyContext.TargetType;
            var propertyType  = propertyContext.TargetProperty;

            if (!classType.HasProperty(PropertyName))
            {
                throw new InvalidProgramException($"The class {classType.FullName} doesn't have the property {PropertyName}.");
            }

            var bindingFlags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;

            var proxyPropertyName = ProxyPropertyName ?? propertyType.Name;
            var property          = classType.GetProperty(PropertyName, bindingFlags);
            var proxyPropetyType  = property.PropertyType;

            if (!proxyPropetyType.HasProperty(proxyPropertyName))
            {
                throw new InvalidProgramException($"The class {proxyPropetyType.FullName} of property {PropertyName} doesn't have the property {proxyPropertyName}.");
            }

            var propertyValue           = property.GetValue(classInstance);
            var propertyOfProxyProperty = proxyPropetyType.GetProperty(proxyPropertyName, bindingFlags);

            // Getter
            if (propertyContext.IsGetter)
            {
                propertyContext.Proceed();
                propertyContext.ReturnValue = propertyOfProxyProperty.GetValue(propertyValue);
                return;
            }

            // Setter
            propertyOfProxyProperty.SetValue(propertyValue, propertyContext.Value);
            propertyContext.Proceed();
        }
Beispiel #7
0
 /// <summary>
 /// Implements advice logic.
 ///             Usually, advice must invoke context.Proceed()
 /// </summary>
 /// <param name="context">The method advice context.</param>
 void IPropertyAdvice.Advise(PropertyAdviceContext context)
 {
     if (context.IsSetter && context.Value == null)
     {
         Logging.DebugMessage("Property '{0}' cannot be null on '{1}'{2}Parent object: {2}{3}", context.TargetProperty.Name, context.TargetType.FullName, Environment.NewLine, context.Target == null ? "" : context.Target.ToString());
         throw new ArgumentNullException(context.TargetProperty.Name, string.Format("Property '{0}' cannot be null on '{1}'", context.TargetProperty.Name, context.TargetType.FullName));
     }
     context.Proceed();
     if (context.IsGetter && CheckOnGet && (!context.HasReturnValue || context.ReturnValue == null))
     {
         Logging.DebugMessage("Property '{0}' cannot be null on '{1}'{2}Parent object: {2}{3}", context.TargetProperty.Name, context.TargetType.FullName, Environment.NewLine, context.Target == null ? "" : context.Target.ToString());
         if (ThrowOnGet)
         {
             throw new ArgumentNullException(context.TargetProperty.Name, string.Format("Property '{0}' cannot be null on '{1}'", context.TargetProperty.Name, context.TargetType.FullName));
         }
     }
 }
 void IPropertyAdvice.Advise(PropertyAdviceContext context)
 {
     try
     {
         context.Proceed();
     }
     catch (NullReferenceException ex)
     {
         ex.Log(string.Format("Null reference in {2}.{0}.[{1}]", context.TargetProperty.Name, context.IsGetter ? "get" : "set", context.TargetType.FullName));
         throw;
     }
     catch (Exception ex)
     {
         ex.Log(context.IsGetter ? "Getter" : "Setter");
         throw;
     }
 }
Beispiel #9
0
 public void Advise(PropertyAdviceContext context)
 {
     context.Proceed();
 }