0
votes

From google, a lot of articles said that DependencyProperty is static because it has a KeyValue mechanism to maintain the value of each instance of the object.

But the problem is that, if we call GetValue / SetValue against the DependencyProperty, how does it identify each instance and generate the key so as to read / save the values from the HashTable for different instance of the object?

For example: if we create 2 instance of TestDp and then set value for TestDProperty of both instances, how does SetValue identify each instance and save the DependencyProperty value into the hash table accordingly?

I've checked the code for GetValue & SetValue of DependencyObject, but I still cannot figure out how it distinguish each instance. The code this.LookupEntry(dp.GlobalIndex) will pickup the EntryIndex but I'm not sure how the GlobalIndex is generated to distinguish each instance of the object.

 public class TestDp : DependencyObject
    {
        protected static DependencyProperty dpTest = DependencyProperty.Register("TestDProperty", typeof(string), typeof(TestDp));
        public string TestDProperty
        {
            get
            {
                var r = (string)GetValue(dpTest);
                return r;
            }
            set
            {
                SetValue(dpTest, value);
            }
        }
    }
1
Not sure I understand your question but you call GetValue/SetValue on current instance of DependencyObject. Also dpTest should be named TestDProperty and CLR wrapper should be called TestD and name of registered property "TestD" - dkozl
set value for TestDProperty of both instances when you do this, you of course have to call the SetValue on different instances, so it knows where to save the data separately. Note that the data are shared only when they have the default value, once they change for some specific instance, they will no longer be shared. - King King
I've checked the code for GetValue & SetValue of DependencyObject, but I still cannot figure out how it distinguish each instance. The code this.LookupEntry(dp.GlobalIndex) will pickup the EntryIndex but I'm not sure how the GlobalIndex is generated to distinguish each instance of the object. - mind1n
@mind1n DependencyProperty definition is static but this.GetValue(...) and this.SetValue(...) are instance methods of DependencyObject so will be called on current instance just as this.LookupEntry(...) - dkozl

1 Answers

1
votes

The GetValue of DependencyObject will call GetValueEntry and consume the EntryIndex which you've mentioned. However, it doesn't seems to generate any key based on the instance information.