WF中DependencyObject和DependencyProperty的實現

WFDependencyObjectDependencyProperty的實現

 

DependencyPropertyRegisterRegisterAttached方法,將DependencyProperty存在IDictionary中完成註冊,確保相同nameDependencyProperty在一個ownerType類型中只能有一個。

DependencyObjectGetValueSetValue的值存在IDictionary中。並預留了GetValueOverride SetValueOverride的處理。

 

Reflector看了一下源碼,下面是源碼的片段

 

[Serializable]

public sealed class DependencyProperty : ISerializable

{

    private static IDictionary<int, DependencyProperty> dependencyProperties;

 

   public static DependencyProperty Register(string name, Type propertyType, Type ownerType)

    {

        return ValidateAndRegister(name, propertyType, ownerType, null, null, true);

    }

 

    public static DependencyProperty RegisterAttached(string name, Type propertyType, Type ownerType)

    {

        return ValidateAndRegister(name, propertyType, ownerType, null, null, false);

    }

 

    //RegisterRegisterAttached調用ValidateAndRegister完成註冊

    private static DependencyProperty ValidateAndRegister(string name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata, Type validatorType, bool isRegistered)

    {

        // Check ...

        // ...

 

        DependencyProperty property = new DependencyProperty(name, propertyType, ownerType, metadata, validatorType, isRegistered);

        lock (((ICollection)dependencyProperties).SyncRoot)

        {

            // 使用HashCode作爲Key

            if (dependencyProperties.ContainsKey(property.GetHashCode()))

            {

                throw new InvalidOperationException(SR.GetString("Error_DPAlreadyExist", new object[] { name, ownerType.FullName }));

            }

            dependencyProperties.Add(property.GetHashCode(), property);

        }

        return property;

    }

 

    // HashCodenameownerTypeHashCode生產,確保相同nameownerTypeDependencyProperty只能有一個。

    public override int GetHashCode()

    {

        return (this.name.GetHashCode() ^ this.ownerType.GetHashCode());

    }

}

 

[DesignerSerializer(typeof(DependencyObjectCodeDomSerializer), typeof(CodeDomSerializer)), DesignerSerializer(typeof(WorkflowMarkupSerializer), typeof(WorkflowMarkupSerializer))]

public abstract class DependencyObject : IComponent, IDependencyObjectAccessor, IDisposable

{

    private IDictionary<DependencyProperty, object> dependencyPropertyValues;

 

    public object GetValue(DependencyProperty dependencyProperty)

    {

        // Check...

        // ...

 

        PropertyMetadata defaultMetadata = dependencyProperty.DefaultMetadata;

 

        // GetValueOverride的處理

        if (defaultMetadata.GetValueOverride != null)

        {

            return defaultMetadata.GetValueOverride(this);

        }

        return this.GetValueCommon(dependencyProperty, defaultMetadata);

    }

 

    private object GetValueCommon(DependencyProperty dependencyProperty, PropertyMetadata metadata)

    {

        object boundValue;

        this.dependencyPropertyValues.TryGetValue(dependencyProperty, out boundValue);

 

        // Others...

 

        return boundValue;

    }

 

    public void SetValue(DependencyProperty dependencyProperty, object value)

    {

        // Check...

        // ...

 

        PropertyMetadata defaultMetadata = dependencyProperty.DefaultMetadata;

        this.SetValueCommon(dependencyProperty, value, defaultMetadata, true);

    }

 

    internal void SetValueCommon(DependencyProperty dependencyProperty, object value, PropertyMetadata metadata, bool shouldCallSetValueOverrideIfExists)

    {

        // Check...

        // ...

 

        // SetValueOverride的處理

        if (shouldCallSetValueOverrideIfExists && (metadata.SetValueOverride != null))

        {

            metadata.SetValueOverride(this, value);

        }

        else

        {

            // Others...

            if (dependencyPropertyValues.ContainsKey(dependencyProperty))

            {

                dependencyPropertyValues[dependencyProperty] = value;

            }

            else

            {

                dependencyPropertyValues.Add(dependencyProperty, value);

            }

        }

    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章