Sunday, January 22, 2012

Poor Man's Implementation of Dependency Property

Points to note:
* Attached event has IsRegistered=false.
* Key of a dependency property = PropertyName ^ OwnerType.
* Check for a static field with name = PropertyName + Property.


   public class MockDependencyProperty
    {
        private static readonly Dictionary _hashCode2MockDependencyProperty;
        private readonly bool _isEvent;
        private readonly bool _isRegistered;
        private readonly Type _ownerType;
        private readonly string _propertyName;
        private readonly Type _propertyType;

        static MockDependencyProperty()
        {
            _hashCode2MockDependencyProperty = new Dictionary();
        }

        private MockDependencyProperty(string propertyName, Type propertyType, Type ownerType, PropertyMetadata metadata,
                                       Type validatorType, bool isRegistered)
        {
            _propertyName = propertyName;
            _ownerType = ownerType;
            _propertyType = propertyType;
            _isRegistered = isRegistered;
            //Out of scope for this discussion
            _isEvent = typeof (Delegate).IsAssignableFrom(_propertyType);
        }

   
        public static MockDependencyProperty Register(string name, Type propertyType, Type parentType)
        {
            return ValidateAndRegister(name, propertyType, parentType, null, null, true);
        }

        public static MockDependencyProperty RegisterAttached(string name, Type propertyType, Type parentType)
        {
            return ValidateAndRegister(name, propertyType, parentType, null, null, false);
        }

        private static MockDependencyProperty ValidateAndRegister(string name, Type propertyType, Type ownerType,
                                                                  PropertyMetadata defaultMetadata, Type validatorType,
                                                                  bool isRegistered)
        {
            //All Null Checks

            FieldInfo field = ownerType.GetField(name + "Property",
                                                 BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Public |
                                                 BindingFlags.Static | BindingFlags.DeclaredOnly);
            if (field == null)
            {
                throw new ArgumentException("Error_DynamicPropertyNotSupported" + ownerType.FullName + name);
            }

            var property = new MockDependencyProperty(name, propertyType, ownerType, defaultMetadata, validatorType,
                                                      isRegistered);
            lock (((ICollection) _hashCode2MockDependencyProperty).SyncRoot)
            {
                if (_hashCode2MockDependencyProperty.ContainsKey(property.GetHashCode()))
                {
                    throw new InvalidOperationException("Error_DPAlreadyExist" + ownerType.FullName);
                }
                _hashCode2MockDependencyProperty.Add(property.GetHashCode(), property);
            }
            return property;
        }


        public override int GetHashCode()
        {
            string propertyName = _propertyName;
            Type ownerType = _ownerType;
            return GetHashCode(propertyName, ownerType);
        }

        private static int GetHashCode(string propertyName, Type ownerType)
        {
            return propertyName.GetHashCode() ^ ownerType.GetHashCode();
        }

        //Properties
    }

No comments:

Post a Comment