.NET 面试系列之WPF(一)

最近又要找工作,很多东西要复习。打算写下面试时的复习的东西做成一个系列,先从WPF开始

DependencyProperty

  • The diffirenc between DependencyProperty and CLR Property

     The CLR property is just a wrapper around private variables.

     The idea of DependencyProperty is to compute the value of property based on external inputs

l  The simple prototype of DependyProperty

Public class DependencyProperty

{

     Internal static Dictionary<object, DependencyProperty> RegisteredDps=new Dictionary<object,DependencyProperty>();

   Internal string Name;

   Internal object Value;

     Internal object HashCode;

  Private DependencyProperty(string name,Type propertyType, Type ownerType, object defaultValue)

     {

          This.Name=name;

          This.Value=defaultValue;

          This.HashCode=name.GetHashCode^ownerType.GetHashCode;

}

Public static DependencyProperty Register(string name,Type propertyType, Type ownerType,object defaultValue)

{

      DependencyProperty dp=new DependencyProperty(name,propertyType,ownerType,defaultValue);

      RegisterdDps.Add(dp.HashCode,dp);

    Return dp;

}

}

 

Public class DependencyObject

{

     Public static readonly DependencyProperty NameProperty=DependencyProperty.Register(“name”,typeof(string),typeof(DependencyObject),string.empty);

 

     Public object GetValue(DependencyProperty dp)

     {

          Return DependencyProperty.RegistedDps[dp.HashCode].Value;

}

Public object SetValue(DependencyProperty dp,object value)

{

      DependencyProperty.RegistedDps[dp.HashCode].Value=value;

}

 

Public string Name

{

       get{return GetValue(NameProperty).ToString()}

      set{SetValue(NameProperty,value);}

}

}

All DependencyObjects share one DependencyProperty, if one object changed dp value, all the others will change.

So the DependencyObject Add effective conception

Improved DependencyObject with _effectiveValue

 

Public class DependencyProperty

{

     Internal static Dictionary<object, DependencyProperty> RegisteredDps=new Dictionary<object,DependencyProperty>();

   Internal string Name;

     Internal int Index;

   Internal object Value;

     Internal object HashCode;

   Private static int globalIndex=0;

  Private DependencyProperty(string name,Type propertyType, Type ownerType, object defaultValue)

     {

          This.Name=name;

          This.Value=defaultValue;

          This.HashCode=name.GetHashCode^ownerType.GetHashCode;

}

Public static DependencyProperty Register(string name,Type propertyType, Type ownerType,object defaultValue)

{

      DependencyProperty dp=new DependencyProperty(name,propertyType,ownerType,defaultValue);

    globalIndex++;

    dp.Index=globalIndex;

      RegisterdDps.Add(dp.HashCode,dp);

    Return dp;

}

}

 

 

Internal struct EffectiveValueEntry

{

       Internal int PropertyIndex{get;set;}

       Internal object Value{get;set;}

}

Public class DependencyObject

{

Private List<EffectiveValueEntry> effectiveValues=new List<EffectiveValueEntry>();

     Public static readonly DependencyProperty NameProperty=DependencyProperty.Register(“name”,typeof(string),typeof(DependencyObject),string.empty);

 

     Public object GetValue(DependencyProperty dp)

     {

          EffectiveValueEntry effectiveValue=effectiveValues.FirstOrDefault((i)=>i.PropertyIndex=dp.Index);

          If(effectiveValue.PropertyIndex!=0)

          {

                Return effectiveValue.Value;

}

Else

{

          Return DependencyProperty.RegistedDps[dp.HashCode].Value;

}

}

Public object SetValue(DependencyProperty dp,object value)

{

EffectiveValueEntry effectiveValue=effectiveValues.FirstOrDefault((i)=>i.PropertyIndex=dp.Index);

          If(effectiveValue.PropertyIndex!=0)

          {

                effectiveValue.Value=value;

}

      Else

      {

EffectiveValueEntry effectiveValue=new EffectiveValueEntry(){PropertyIndex=dp.Index; Value=value;}

}

      }

 

Public string Name

{

       get{return GetValue(NameProperty).ToString()}

      set{SetValue(NameProperty,value);}

}

}

Problem: Derived class can override field in parent class

Improved

Public class PropertyMetaData

{

         Public Type Type{get;set;};

         Public object Value{get;set;}

         Public PropertyMetaData(object defaultValue)

         {

                   This.Value=defaultValue;

}

}

 

Public class DependencyProperty

{

     Internal static Dictionary<object, DependencyProperty> RegisteredDps=new Dictionary<object,DependencyProperty>();

   Internal string Name;

     Internal int Index;

   Internal object Value;

     Internal object HashCode;

   Private static int globalIndex=0;

     Private List<PropertyMetaData> metaDataMap=new List<PropertyMetaData>();

Private PropertyMetaData defaultMetaData;

  Private DependencyProperty(string name,Type propertyType, Type ownerType, object defaultValue)

     {

          This.Name=name;

          This.Value=defaultValue;

          This.HashCode=name.GetHashCode^ownerType.GetHashCode;

          PropertyMetaData metadata=new PropertyMetaData(defaultValue){Type=ownerType};

          metaDataMap.Add(metaData);

          defaultMetaData=metadata;

}

Public static DependencyProperty Register(string name,Type propertyType, Type ownerType,object defaultValue)

{

      DependencyProperty dp=new DependencyProperty(name,propertyType,ownerType,defaultValue);

    globalIndex++;

    dp.Index=globalIndex;

      RegisterdDps.Add(dp.HashCode,dp);

    Return dp;

}

 

Public void OverrideMetaData(Type forType,PropertyMetaData metaData)

{

      metadata.Type=forType;

      metaDataMap.Add(metaData);

}

 

Public PropertyMetaData GetMetaData(Type type)

{

      PropertyMetaData metadata=metaDataMap.FirstOrDefault((i)=>type.isSubClassOf(i.Type));

      If(metadata==null)

           Return defaultMetaData;

      Else

           Return metadata;

}

}

 

 

Public class DependencyObject

{

Private List<EffectiveValueEntry> effectiveValues=new List<EffectiveValueEntry>();

     Public static readonly DependencyProperty NameProperty=DependencyProperty.Register(“name”,typeof(string),typeof(DependencyObject),string.empty);

 

     Public object GetValue(DependencyProperty dp)

     {

          EffectiveValueEntry effectiveValue=effectiveValues.FirstOrDefault((i)=>i.PropertyIndex=dp.Index);

          If(effectiveValue.PropertyIndex!=0)

          {

                Return effectiveValue.Value;

}

Else

{

          Return DependencyProperty.RegistedDps[dp.HashCode].GetMetaData(this.getType()).Value;

}

}

Public object SetValue(DependencyProperty dp,object value)

{

EffectiveValueEntry effectiveValue=effectiveValues.FirstOrDefault((i)=>i.PropertyIndex=dp.Index);

          If(effectiveValue.PropertyIndex!=0)

          {

                effectiveValue.Value=value;

}

      Else

      {

EffectiveValueEntry effectiveValue=new EffectiveValueEntry(){PropertyIndex=dp.Index; Value=value;}

}

      }

 

Public string Name

{

       get{return GetValue(NameProperty).ToString()}

      set{SetValue(NameProperty,value);}

}

}

Then we can define a new DependencyObject instance

Public class SubDenpendencyObject:DependencyObject

{

         Static SubDependencyObject()

         {

                   NameProperty.OverrideMetaData(typeof(SubDependencyProperty),new PropertyMetaData(“SubName”));

}

}

http://www.cnblogs.com/Zhouyongh/archive/2009/09/10/1564099.html

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。