PostSharp 2.1 Starter Edition

Apr 11, 2012 at 9:18 AM

I've update my code to make use of the Free Starter Edition of PostSharp 2.1.  It does not support Associations (XPCollection properties).

    [Serializable]
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
    [MulticastAttributeUsage(MulticastTargets.Property, //Inheritance = MulticastInheritance.Multicast,
        TargetMemberAttributes = MulticastAttributes.NonAbstract | MulticastAttributes.Public)]
    public sealed class XpoAutomaticPropertiesAttribute : LocationInterceptionAspect
    {
        public MethodInfo OnSetPropertyValueMethod;

        public override void OnSetValue(LocationInterceptionArgs args)
        {
            // Don't go further if the new value is equal to the old one.
            // Use object.Equals, because == operator on objects always checks reference equality, but object.Equals() is virtual
            if ((args.Value == null) && (args.GetCurrentValue() == null))
                return;
            if ((args.Value != null) && (args.GetCurrentValue() != null) && args.Value.Equals(args.GetCurrentValue()))
                return;

            args.ProceedSetValue();

            var ignoreAttributes = (XpoIgnoreAutomaticPropertyAttribute[])args.Location.PropertyInfo.GetCustomAttributes(typeof(XpoIgnoreAutomaticPropertyAttribute), true);
            if (ignoreAttributes.Any())
                return;

            // Invoke method OnSetPropertyValue (our, the base one, or the overridden one).
            OnSetPropertyValueMethod.Invoke(args.Instance, new object[] { args.Location.Name, args.Value });
        }

        public override void CompileTimeInitialize(PostSharp.Reflection.LocationInfo locationInfo, AspectInfo aspectInfo)
        {
            OnSetPropertyValueMethod = locationInfo.DeclaringType.GetMethod(
                "SetPropertyValue", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                null,
                new Type[] { typeof(string), typeof(object) },
                null);
        }
    }