none
VS2012如何能够看到.net类库的源代码? RRS feed

答案

  • 考虑下载Reflector可以动态源代码调试。

    或者考虑下载JustDe-compiler,IL Spy等工具。

    这里给出源码:

    namespace System.Windows
    {
      [TypeConverter("System.Windows.Markup.DependencyPropertyConverter, PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")]
      [ValueSerializer(typeof (DependencyPropertyValueSerializer))]
      public sealed class DependencyProperty
      {
        public static readonly object UnsetValue;
        private string _name;
        private Type _propertyType;
        private Type _ownerType;
        private PropertyMetadata _defaultMetadata;
        private ValidateValueCallback _validateValueCallback;
        private DependencyPropertyKey _readOnlyKey;
        private DependencyProperty.Flags _packedData;
        internal InsertionSortMap _metadataMap;
        private CoerceValueCallback _designerCoerceValueCallback;
        internal static ItemStructList<DependencyProperty> RegisteredPropertyList;
        private static Hashtable PropertyFromName;
        private static int GlobalIndexCount;
        internal static object Synchronized;
        private static Type NullableType;
    
        public string Name
        {
          get
          {
            return this._name;
          }
        }
    
        public Type PropertyType
        {
          get
          {
            return this._propertyType;
          }
        }
    
        public Type OwnerType
        {
          get
          {
            return this._ownerType;
          }
        }
    
        public PropertyMetadata DefaultMetadata
        {
          get
          {
            return this._defaultMetadata;
          }
        }
    
        public ValidateValueCallback ValidateValueCallback
        {
          get
          {
            return this._validateValueCallback;
          }
        }
    
        public int GlobalIndex
        {
          get
          {
            return (int) (this._packedData & DependencyProperty.Flags.GlobalIndexMask);
          }
        }
    
        internal bool IsObjectType
        {
          get
          {
            return (this._packedData & DependencyProperty.Flags.IsObjectType) != (DependencyProperty.Flags) 0;
          }
        }
    
        internal bool IsValueType
        {
          get
          {
            return (this._packedData & DependencyProperty.Flags.IsValueType) != (DependencyProperty.Flags) 0;
          }
        }
    
        internal bool IsFreezableType
        {
          get
          {
            return (this._packedData & DependencyProperty.Flags.IsFreezableType) != (DependencyProperty.Flags) 0;
          }
        }
    
        internal bool IsStringType
        {
          get
          {
            return (this._packedData & DependencyProperty.Flags.IsStringType) != (DependencyProperty.Flags) 0;
          }
        }
    
        internal bool IsPotentiallyInherited
        {
          get
          {
            return (this._packedData & DependencyProperty.Flags.IsPotentiallyInherited) != (DependencyProperty.Flags) 0;
          }
        }
    
        internal bool IsDefaultValueChanged
        {
          get
          {
            return (this._packedData & DependencyProperty.Flags.IsDefaultValueChanged) != (DependencyProperty.Flags) 0;
          }
        }
    
        internal bool IsPotentiallyUsingDefaultValueFactory
        {
          get
          {
            return (this._packedData & DependencyProperty.Flags.IsPotentiallyUsingDefaultValueFactory) != (DependencyProperty.Flags) 0;
          }
        }
    
        public bool ReadOnly
        {
          get
          {
            return this._readOnlyKey != null;
          }
        }
    
        internal CoerceValueCallback DesignerCoerceValueCallback
        {
          get
          {
            return this._designerCoerceValueCallback;
          }
          set
          {
            if (this.ReadOnly)
              throw new InvalidOperationException(System.Windows.SR.Get("ReadOnlyDesignerCoersionNotAllowed", new object[1]
              {
                (object) this.Name
              }));
            else
              this._designerCoerceValueCallback = value;
          }
        }
    
        internal static int RegisteredPropertyCount
        {
          get
          {
            return DependencyProperty.RegisteredPropertyList.Count;
          }
        }
    
        internal static IEnumerable RegisteredProperties
        {
          get
          {
            return (IEnumerable) new DependencyProperty.RegisteredPropertiesEnumerable(-2);
          }
        }
    
        static DependencyProperty()
        {
          DependencyProperty.UnsetValue = (object) new NamedObject("DependencyProperty.UnsetValue");
          DependencyProperty.RegisteredPropertyList = new ItemStructList<DependencyProperty>(768);
          DependencyProperty.PropertyFromName = new Hashtable();
          DependencyProperty.Synchronized = new object();
          DependencyProperty.NullableType = typeof (Nullable<>);
        }
    
        private DependencyProperty(string name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata, ValidateValueCallback validateValueCallback)
        {
          this._metadataMap = new InsertionSortMap();
          base.\u002Ector();
          this._name = name;
          this._propertyType = propertyType;
          this._ownerType = ownerType;
          this._defaultMetadata = defaultMetadata;
          this._validateValueCallback = validateValueCallback;
          object obj;
          Monitor.Enter(obj = DependencyProperty.Synchronized);
          DependencyProperty.Flags flags;
          try
          {
            flags = (DependencyProperty.Flags) DependencyProperty.GetUniqueGlobalIndex(ownerType, name);
            DependencyProperty.RegisteredPropertyList.Add(this);
          }
          finally
          {
            Monitor.Exit(obj);
          }
          if (propertyType.IsValueType)
            flags |= DependencyProperty.Flags.IsValueType;
          if (propertyType == typeof (object))
            flags |= DependencyProperty.Flags.IsObjectType;
          if (typeof (Freezable).IsAssignableFrom(propertyType))
            flags |= DependencyProperty.Flags.IsFreezableType;
          if (propertyType == typeof (string))
            flags |= DependencyProperty.Flags.IsStringType;
          this._packedData = flags;
        }
    
        public static DependencyProperty Register(string name, Type propertyType, Type ownerType)
        {
          return DependencyProperty.Register(name, propertyType, ownerType, (PropertyMetadata) null, (ValidateValueCallback) null);
        }
    
        public static DependencyProperty Register(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata)
        {
          return DependencyProperty.Register(name, propertyType, ownerType, typeMetadata, (ValidateValueCallback) null);
        }
    
        public static DependencyProperty Register(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata, ValidateValueCallback validateValueCallback)
        {
          DependencyProperty.RegisterParameterValidation(name, propertyType, ownerType);
          PropertyMetadata defaultMetadata = (PropertyMetadata) null;
          if (typeMetadata != null && typeMetadata.DefaultValueWasSet())
            defaultMetadata = new PropertyMetadata(typeMetadata.DefaultValue);
          DependencyProperty dependencyProperty = DependencyProperty.RegisterCommon(name, propertyType, ownerType, defaultMetadata, validateValueCallback);
          if (typeMetadata != null)
            dependencyProperty.OverrideMetadata(ownerType, typeMetadata);
          return dependencyProperty;
        }
    
        public static DependencyPropertyKey RegisterReadOnly(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata)
        {
          return DependencyProperty.RegisterReadOnly(name, propertyType, ownerType, typeMetadata, (ValidateValueCallback) null);
        }
    
        public static DependencyPropertyKey RegisterReadOnly(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata, ValidateValueCallback validateValueCallback)
        {
          DependencyProperty.RegisterParameterValidation(name, propertyType, ownerType);
          PropertyMetadata defaultMetadata = typeMetadata == null || !typeMetadata.DefaultValueWasSet() ? DependencyProperty.AutoGeneratePropertyMetadata(propertyType, validateValueCallback, name, ownerType) : new PropertyMetadata(typeMetadata.DefaultValue);
          DependencyPropertyKey key = new DependencyPropertyKey((DependencyProperty) null);
          DependencyProperty dp = DependencyProperty.RegisterCommon(name, propertyType, ownerType, defaultMetadata, validateValueCallback);
          dp._readOnlyKey = key;
          key.SetDependencyProperty(dp);
          if (typeMetadata == null)
            typeMetadata = DependencyProperty.AutoGeneratePropertyMetadata(propertyType, validateValueCallback, name, ownerType);
          dp.OverrideMetadata(ownerType, typeMetadata, key);
          return key;
        }
    
        public static DependencyPropertyKey RegisterAttachedReadOnly(string name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata)
        {
          return DependencyProperty.RegisterAttachedReadOnly(name, propertyType, ownerType, defaultMetadata, (ValidateValueCallback) null);
        }
    
        public static DependencyPropertyKey RegisterAttachedReadOnly(string name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata, ValidateValueCallback validateValueCallback)
        {
          DependencyProperty.RegisterParameterValidation(name, propertyType, ownerType);
          if (defaultMetadata == null)
            defaultMetadata = DependencyProperty.AutoGeneratePropertyMetadata(propertyType, validateValueCallback, name, ownerType);
          DependencyPropertyKey dependencyPropertyKey = new DependencyPropertyKey((DependencyProperty) null);
          DependencyProperty dp = DependencyProperty.RegisterCommon(name, propertyType, ownerType, defaultMetadata, validateValueCallback);
          dp._readOnlyKey = dependencyPropertyKey;
          dependencyPropertyKey.SetDependencyProperty(dp);
          return dependencyPropertyKey;
        }
    
        public static DependencyProperty RegisterAttached(string name, Type propertyType, Type ownerType)
        {
          return DependencyProperty.RegisterAttached(name, propertyType, ownerType, (PropertyMetadata) null, (ValidateValueCallback) null);
        }
    
        public static DependencyProperty RegisterAttached(string name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata)
        {
          return DependencyProperty.RegisterAttached(name, propertyType, ownerType, defaultMetadata, (ValidateValueCallback) null);
        }
    
        public static DependencyProperty RegisterAttached(string name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata, ValidateValueCallback validateValueCallback)
        {
          DependencyProperty.RegisterParameterValidation(name, propertyType, ownerType);
          return DependencyProperty.RegisterCommon(name, propertyType, ownerType, defaultMetadata, validateValueCallback);
        }
    
        private static void RegisterParameterValidation(string name, Type propertyType, Type ownerType)
        {
          if (name == null)
            throw new ArgumentNullException("name");
          if (name.Length == 0)
            throw new ArgumentException(System.Windows.SR.Get("StringEmpty"), "name");
          if (ownerType == null)
            throw new ArgumentNullException("ownerType");
          if (propertyType == null)
            throw new ArgumentNullException("propertyType");
        }
    
        private static DependencyProperty RegisterCommon(string name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata, ValidateValueCallback validateValueCallback)
        {
          DependencyProperty.FromNameKey fromNameKey = new DependencyProperty.FromNameKey(name, ownerType);
          object obj1;
          Monitor.Enter(obj1 = DependencyProperty.Synchronized);
          try
          {
            if (DependencyProperty.PropertyFromName.Contains((object) fromNameKey))
              throw new ArgumentException(System.Windows.SR.Get("PropertyAlreadyRegistered", (object) name, (object) ownerType.Name));
          }
          finally
          {
            Monitor.Exit(obj1);
          }
          if (defaultMetadata == null)
          {
            defaultMetadata = DependencyProperty.AutoGeneratePropertyMetadata(propertyType, validateValueCallback, name, ownerType);
          }
          else
          {
            if (!defaultMetadata.DefaultValueWasSet())
              defaultMetadata.DefaultValue = DependencyProperty.AutoGenerateDefaultValue(propertyType);
            DependencyProperty.ValidateMetadataDefaultValue(defaultMetadata, propertyType, name, validateValueCallback);
          }
          DependencyProperty dp = new DependencyProperty(name, propertyType, ownerType, defaultMetadata, validateValueCallback);
          defaultMetadata.Seal(dp, (Type) null);
          if (defaultMetadata.IsInherited)
            dp._packedData |= DependencyProperty.Flags.IsPotentiallyInherited;
          if (defaultMetadata.UsingDefaultValueFactory)
            dp._packedData |= DependencyProperty.Flags.IsPotentiallyUsingDefaultValueFactory;
          object obj2;
          Monitor.Enter(obj2 = DependencyProperty.Synchronized);
          try
          {
            DependencyProperty.PropertyFromName[(object) fromNameKey] = (object) dp;
          }
          finally
          {
            Monitor.Exit(obj2);
          }
          if (TraceDependencyProperty.IsEnabled)
            TraceDependencyProperty.TraceActivityItem(TraceDependencyProperty.Register, (object) dp, (object) dp.OwnerType);
          return dp;
        }
    
        private static object AutoGenerateDefaultValue(Type propertyType)
        {
          object obj = (object) null;
          if (propertyType.IsValueType)
            obj = Activator.CreateInstance(propertyType);
          return obj;
        }
    
        private static PropertyMetadata AutoGeneratePropertyMetadata(Type propertyType, ValidateValueCallback validateValueCallback, string name, Type ownerType)
        {
          object defaultValue = DependencyProperty.AutoGenerateDefaultValue(propertyType);
          if (validateValueCallback == null || validateValueCallback(defaultValue))
            return new PropertyMetadata(defaultValue);
          throw new ArgumentException(System.Windows.SR.Get("DefaultValueAutoAssignFailed", (object) name, (object) ownerType.Name));
        }
    
        private static void ValidateMetadataDefaultValue(PropertyMetadata defaultMetadata, Type propertyType, string propertyName, ValidateValueCallback validateValueCallback)
        {
          if (defaultMetadata.UsingDefaultValueFactory)
            return;
          DependencyProperty.ValidateDefaultValueCommon(defaultMetadata.DefaultValue, propertyType, propertyName, validateValueCallback, true);
        }
    
        internal void ValidateFactoryDefaultValue(object defaultValue)
        {
          DependencyProperty.ValidateDefaultValueCommon(defaultValue, this.PropertyType, this.Name, this.ValidateValueCallback, false);
        }
    
        private static void ValidateDefaultValueCommon(object defaultValue, Type propertyType, string propertyName, ValidateValueCallback validateValueCallback, bool checkThreadAffinity)
        {
          if (!DependencyProperty.IsValidType(defaultValue, propertyType))
          {
            throw new ArgumentException(System.Windows.SR.Get("DefaultValuePropertyTypeMismatch", new object[1]
            {
              (object) propertyName
            }));
          }
          else
          {
            if (defaultValue is Expression)
              throw new ArgumentException(System.Windows.SR.Get("DefaultValueMayNotBeExpression"));
            if (checkThreadAffinity)
            {
              DispatcherObject dispatcherObject = defaultValue as DispatcherObject;
              if (dispatcherObject != null && dispatcherObject.Dispatcher != null)
              {
                ISealable sealable = dispatcherObject as ISealable;
                if (sealable != null && sealable.CanSeal)
                {
                  Invariant.Assert(!sealable.IsSealed, "A Sealed ISealable must not have dispatcher affinity");
                  sealable.Seal();
                  Invariant.Assert(dispatcherObject.Dispatcher == null, "ISealable.Seal() failed after ISealable.CanSeal returned true");
                }
                else
                  throw new ArgumentException(System.Windows.SR.Get("DefaultValueMustBeFreeThreaded", new object[1]
                  {
                    (object) propertyName
                  }));
              }
            }
            if (validateValueCallback == null || validateValueCallback(defaultValue))
              return;
            throw new ArgumentException(System.Windows.SR.Get("DefaultValueInvalid", new object[1]
            {
              (object) propertyName
            }));
          }
        }
    
        private void SetupOverrideMetadata(Type forType, PropertyMetadata typeMetadata, out DependencyObjectType dType, out PropertyMetadata baseMetadata)
        {
          if (forType == null)
            throw new ArgumentNullException("forType");
          if (typeMetadata == null)
            throw new ArgumentNullException("typeMetadata");
          if (typeMetadata.Sealed)
            throw new ArgumentException(System.Windows.SR.Get("TypeMetadataAlreadyInUse"));
          if (!typeof (DependencyObject).IsAssignableFrom(forType))
          {
            throw new ArgumentException(System.Windows.SR.Get("TypeMustBeDependencyObjectDerived", new object[1]
            {
              (object) forType.Name
            }));
          }
          else
          {
            if (typeMetadata.IsDefaultValueModified)
              DependencyProperty.ValidateMetadataDefaultValue(typeMetadata, this.PropertyType, this.Name, this.ValidateValueCallback);
            dType = DependencyObjectType.FromSystemType(forType);
            baseMetadata = this.GetMetadata(dType.BaseType);
            if (!baseMetadata.GetType().IsAssignableFrom(typeMetadata.GetType()))
              throw new ArgumentException(System.Windows.SR.Get("OverridingMetadataDoesNotMatchBaseMetadataType"));
          }
        }
    
        public void OverrideMetadata(Type forType, PropertyMetadata typeMetadata)
        {
          DependencyObjectType dType;
          PropertyMetadata baseMetadata;
          this.SetupOverrideMetadata(forType, typeMetadata, out dType, out baseMetadata);
          if (this.ReadOnly)
            throw new InvalidOperationException(System.Windows.SR.Get("ReadOnlyOverrideNotAllowed", new object[1]
            {
              (object) this.Name
            }));
          else
            this.ProcessOverrideMetadata(forType, typeMetadata, dType, baseMetadata);
        }
    
        public void OverrideMetadata(Type forType, PropertyMetadata typeMetadata, DependencyPropertyKey key)
        {
          DependencyObjectType dType;
          PropertyMetadata baseMetadata;
          this.SetupOverrideMetadata(forType, typeMetadata, out dType, out baseMetadata);
          if (key == null)
            throw new ArgumentNullException("key");
          if (!this.ReadOnly)
            throw new InvalidOperationException(System.Windows.SR.Get("PropertyNotReadOnly"));
          if (key.DependencyProperty != this)
          {
            throw new ArgumentException(System.Windows.SR.Get("ReadOnlyOverrideKeyNotAuthorized", new object[1]
            {
              (object) this.Name
            }));
          }
          else
          {
            this.VerifyReadOnlyKey(key);
            this.ProcessOverrideMetadata(forType, typeMetadata, dType, baseMetadata);
          }
        }
    
        private void ProcessOverrideMetadata(Type forType, PropertyMetadata typeMetadata, DependencyObjectType dType, PropertyMetadata baseMetadata)
        {
          object obj;
          Monitor.Enter(obj = DependencyProperty.Synchronized);
          try
          {
            if (DependencyProperty.UnsetValue == this._metadataMap[dType.Id])
              this._metadataMap[dType.Id] = (object) typeMetadata;
            else
              throw new ArgumentException(System.Windows.SR.Get("TypeMetadataAlreadyRegistered", new object[1]
              {
                (object) forType.Name
              }));
          }
          finally
          {
            Monitor.Exit(obj);
          }
          typeMetadata.InvokeMerge(baseMetadata, this);
          typeMetadata.Seal(this, forType);
          if (typeMetadata.IsInherited)
            this._packedData |= DependencyProperty.Flags.IsPotentiallyInherited;
          if (typeMetadata.DefaultValueWasSet() && typeMetadata.DefaultValue != this.DefaultMetadata.DefaultValue)
            this._packedData |= DependencyProperty.Flags.IsDefaultValueChanged;
          if (!typeMetadata.UsingDefaultValueFactory)
            return;
          this._packedData |= DependencyProperty.Flags.IsPotentiallyUsingDefaultValueFactory;
        }
    
        [FriendAccessAllowed]
        internal object GetDefaultValue(DependencyObjectType dependencyObjectType)
        {
          if (!this.IsDefaultValueChanged)
            return this.DefaultMetadata.DefaultValue;
          else
            return this.GetMetadata(dependencyObjectType).DefaultValue;
        }
    
        [FriendAccessAllowed]
        internal object GetDefaultValue(Type forType)
        {
          if (!this.IsDefaultValueChanged)
            return this.DefaultMetadata.DefaultValue;
          else
            return this.GetMetadata(DependencyObjectType.FromSystemTypeInternal(forType)).DefaultValue;
        }
    
        public PropertyMetadata GetMetadata(Type forType)
        {
          if (forType != null)
            return this.GetMetadata(DependencyObjectType.FromSystemType(forType));
          else
            throw new ArgumentNullException("forType");
        }
    
        public PropertyMetadata GetMetadata(DependencyObject dependencyObject)
        {
          if (dependencyObject != null)
            return this.GetMetadata(dependencyObject.DependencyObjectType);
          else
            throw new ArgumentNullException("dependencyObject");
        }
    
        public PropertyMetadata GetMetadata(DependencyObjectType dependencyObjectType)
        {
          if (dependencyObjectType != null)
          {
            int index = this._metadataMap.Count - 1;
            if (index < 0)
              return this._defaultMetadata;
            if (index == 0)
            {
              int key;
              object obj;
              this._metadataMap.GetKeyValuePair(index, out key, out obj);
              while (dependencyObjectType.Id > key)
                dependencyObjectType = dependencyObjectType.BaseType;
              if (key == dependencyObjectType.Id)
                return (PropertyMetadata) obj;
            }
            else if (dependencyObjectType.Id != 0)
            {
              do
              {
                int key;
                object obj;
                this._metadataMap.GetKeyValuePair(index, out key, out obj);
                for (--index; dependencyObjectType.Id < key && index >= 0; --index)
                  this._metadataMap.GetKeyValuePair(index, out key, out obj);
                while (dependencyObjectType.Id > key)
                  dependencyObjectType = dependencyObjectType.BaseType;
                if (key == dependencyObjectType.Id)
                  return (PropertyMetadata) obj;
              }
              while (index >= 0);
            }
          }
          return this._defaultMetadata;
        }
    
        public DependencyProperty AddOwner(Type ownerType)
        {
          return this.AddOwner(ownerType, (PropertyMetadata) null);
        }
    
        public DependencyProperty AddOwner(Type ownerType, PropertyMetadata typeMetadata)
        {
          if (ownerType == null)
            throw new ArgumentNullException("ownerType");
          DependencyProperty.FromNameKey fromNameKey = new DependencyProperty.FromNameKey(this.Name, ownerType);
          object obj1;
          Monitor.Enter(obj1 = DependencyProperty.Synchronized);
          try
          {
            if (DependencyProperty.PropertyFromName.Contains((object) fromNameKey))
              throw new ArgumentException(System.Windows.SR.Get("PropertyAlreadyRegistered", (object) this.Name, (object) ownerType.Name));
          }
          finally
          {
            Monitor.Exit(obj1);
          }
          if (typeMetadata != null)
            this.OverrideMetadata(ownerType, typeMetadata);
          object obj2;
          Monitor.Enter(obj2 = DependencyProperty.Synchronized);
          try
          {
            DependencyProperty.PropertyFromName[(object) fromNameKey] = (object) this;
          }
          finally
          {
            Monitor.Exit(obj2);
          }
          return this;
        }
    
        public override int GetHashCode()
        {
          return this.GlobalIndex;
        }
    
        public bool IsValidType(object value)
        {
          return DependencyProperty.IsValidType(value, this.PropertyType);
        }
    
        public bool IsValidValue(object value)
        {
          if (!DependencyProperty.IsValidType(value, this.PropertyType))
            return false;
          if (this.ValidateValueCallback != null)
            return this.ValidateValueCallback(value);
          else
            return true;
        }
    
        internal void VerifyReadOnlyKey(DependencyPropertyKey candidateKey)
        {
          if (this._readOnlyKey != candidateKey)
            throw new ArgumentException(System.Windows.SR.Get("ReadOnlyKeyNotAuthorized"));
        }
    
        internal bool IsValidValueInternal(object value)
        {
          if (this.ValidateValueCallback != null)
            return this.ValidateValueCallback(value);
          else
            return true;
        }
    
        [FriendAccessAllowed]
        internal static DependencyProperty FromName(string name, Type ownerType)
        {
          DependencyProperty dependencyProperty = (DependencyProperty) null;
          if (name == null)
            throw new ArgumentNullException("name");
          if (ownerType == null)
            throw new ArgumentNullException("ownerType");
          DependencyProperty.FromNameKey fromNameKey = new DependencyProperty.FromNameKey(name, ownerType);
          for (; dependencyProperty == null && ownerType != null; ownerType = ownerType.BaseType)
          {
            RuntimeHelpers.RunClassConstructor(ownerType.TypeHandle);
            fromNameKey.UpdateNameKey(ownerType);
            object obj;
            Monitor.Enter(obj = DependencyProperty.Synchronized);
            try
            {
              dependencyProperty = (DependencyProperty) DependencyProperty.PropertyFromName[(object) fromNameKey];
            }
            finally
            {
              Monitor.Exit(obj);
            }
          }
          return dependencyProperty;
        }
    
        public override string ToString()
        {
          return this._name;
        }
    
        internal static bool IsValidType(object value, Type propertyType)
        {
          if (value == null)
          {
            if (propertyType.IsValueType && (!propertyType.IsGenericType || propertyType.GetGenericTypeDefinition() != DependencyProperty.NullableType))
              return false;
          }
          else if (!propertyType.IsInstanceOfType(value))
            return false;
          return true;
        }
    
        internal static int GetUniqueGlobalIndex(Type ownerType, string name)
        {
          if (DependencyProperty.GlobalIndexCount < (int) ushort.MaxValue)
            return DependencyProperty.GlobalIndexCount++;
          if (ownerType != null)
            throw new InvalidOperationException(System.Windows.SR.Get("TooManyDependencyProperties", new object[1]
            {
              (object) (ownerType.Name + "." + name)
            }));
          else
            throw new InvalidOperationException(System.Windows.SR.Get("TooManyDependencyProperties", new object[1]
            {
              (object) "ConstantProperty"
            }));
        }
    
        private class FromNameKey
        {
          private string _name;
          private Type _ownerType;
          private int _hashCode;
    
          public FromNameKey(string name, Type ownerType)
          {
            base.\u002Ector();
            this._name = name;
            this._ownerType = ownerType;
            this._hashCode = this._name.GetHashCode() ^ this._ownerType.GetHashCode();
          }
    
          public void UpdateNameKey(Type ownerType)
          {
            this._ownerType = ownerType;
            this._hashCode = this._name.GetHashCode() ^ this._ownerType.GetHashCode();
          }
    
          public override int GetHashCode()
          {
            return this._hashCode;
          }
    
          public override bool Equals(object o)
          {
            if (o != null && o is DependencyProperty.FromNameKey)
              return this.Equals((DependencyProperty.FromNameKey) o);
            else
              return false;
          }
    
          public bool Equals(DependencyProperty.FromNameKey key)
          {
            if (this._name.Equals(key._name))
              return this._ownerType == key._ownerType;
            else
              return false;
          }
        }
    
        [System.Flags]
        private enum Flags
        {
          GlobalIndexMask = 65535,
          IsValueType = 65536,
          IsFreezableType = 131072,
          IsStringType = 262144,
          IsPotentiallyInherited = 524288,
          IsDefaultValueChanged = 1048576,
          IsPotentiallyUsingDefaultValueFactory = 2097152,
          IsObjectType = 4194304,
        }
    
        private sealed class RegisteredPropertiesEnumerable : IEnumerable<object>, IEnumerable, IEnumerator<object>, IEnumerator, IDisposable
        {
          private int _state;
          private object _current;
          private int _index;
          private int _threadId;
          private DependencyProperty[] _propertyList;
    
          object IEnumerator<object>.Current
          {
            get
            {
              return this._current;
            }
          }
    
          object IEnumerator.Current
          {
            get
            {
              return this._current;
            }
          }
    
          public RegisteredPropertiesEnumerable(int state)
          {
            base.\u002Ector();
            this._state = state;
            this._threadId = Thread.CurrentThread.ManagedThreadId;
          }
    
          IEnumerator<object> IEnumerable<object>.GetEnumerator()
          {
            if (Thread.CurrentThread.ManagedThreadId != this._threadId || this._state != -2)
              return (IEnumerator<object>) new DependencyProperty.RegisteredPropertiesEnumerable(0);
            this._state = 0;
            return (IEnumerator<object>) this;
          }
    
          IEnumerator IEnumerable.GetEnumerator()
          {
            return (IEnumerator) this.GetEnumerator();
          }
    
          void IEnumerator.Reset()
          {
            throw new NotSupportedException();
          }
    
          public bool MoveNext()
          {
            bool flag = true;
            try
            {
              switch (this._state)
              {
                case 0:
                  this._state = 1;
                  this._propertyList = DependencyProperty.RegisteredPropertyList.List;
                  this._index = 0;
                  break;
                case 2:
                  this._state = 1;
                  ++this._index;
                  break;
                default:
                  flag = false;
                  return false;
              }
              for (; this._index < this._propertyList.Length; ++this._index)
              {
                DependencyProperty dependencyProperty = this._propertyList[this._index];
                if (dependencyProperty != null)
                {
                  this._current = (object) dependencyProperty;
                  this._state = 2;
                  flag = false;
                  return true;
                }
              }
              this.Finally2();
              flag = false;
              return false;
            }
            finally
            {
              if (flag)
                this.Dispose();
            }
          }
    
          void IDisposable.Dispose()
          {
            switch (this._state)
            {
              case 1:
              case 2:
                this.Finally2();
                break;
            }
          }
    
          private void Finally2()
          {
            this._state = -1;
          }
        }
      }
    }

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月8日 2:11
    版主

全部回复

  • 考虑下载Reflector可以动态源代码调试。

    或者考虑下载JustDe-compiler,IL Spy等工具。

    这里给出源码:

    namespace System.Windows
    {
      [TypeConverter("System.Windows.Markup.DependencyPropertyConverter, PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")]
      [ValueSerializer(typeof (DependencyPropertyValueSerializer))]
      public sealed class DependencyProperty
      {
        public static readonly object UnsetValue;
        private string _name;
        private Type _propertyType;
        private Type _ownerType;
        private PropertyMetadata _defaultMetadata;
        private ValidateValueCallback _validateValueCallback;
        private DependencyPropertyKey _readOnlyKey;
        private DependencyProperty.Flags _packedData;
        internal InsertionSortMap _metadataMap;
        private CoerceValueCallback _designerCoerceValueCallback;
        internal static ItemStructList<DependencyProperty> RegisteredPropertyList;
        private static Hashtable PropertyFromName;
        private static int GlobalIndexCount;
        internal static object Synchronized;
        private static Type NullableType;
    
        public string Name
        {
          get
          {
            return this._name;
          }
        }
    
        public Type PropertyType
        {
          get
          {
            return this._propertyType;
          }
        }
    
        public Type OwnerType
        {
          get
          {
            return this._ownerType;
          }
        }
    
        public PropertyMetadata DefaultMetadata
        {
          get
          {
            return this._defaultMetadata;
          }
        }
    
        public ValidateValueCallback ValidateValueCallback
        {
          get
          {
            return this._validateValueCallback;
          }
        }
    
        public int GlobalIndex
        {
          get
          {
            return (int) (this._packedData & DependencyProperty.Flags.GlobalIndexMask);
          }
        }
    
        internal bool IsObjectType
        {
          get
          {
            return (this._packedData & DependencyProperty.Flags.IsObjectType) != (DependencyProperty.Flags) 0;
          }
        }
    
        internal bool IsValueType
        {
          get
          {
            return (this._packedData & DependencyProperty.Flags.IsValueType) != (DependencyProperty.Flags) 0;
          }
        }
    
        internal bool IsFreezableType
        {
          get
          {
            return (this._packedData & DependencyProperty.Flags.IsFreezableType) != (DependencyProperty.Flags) 0;
          }
        }
    
        internal bool IsStringType
        {
          get
          {
            return (this._packedData & DependencyProperty.Flags.IsStringType) != (DependencyProperty.Flags) 0;
          }
        }
    
        internal bool IsPotentiallyInherited
        {
          get
          {
            return (this._packedData & DependencyProperty.Flags.IsPotentiallyInherited) != (DependencyProperty.Flags) 0;
          }
        }
    
        internal bool IsDefaultValueChanged
        {
          get
          {
            return (this._packedData & DependencyProperty.Flags.IsDefaultValueChanged) != (DependencyProperty.Flags) 0;
          }
        }
    
        internal bool IsPotentiallyUsingDefaultValueFactory
        {
          get
          {
            return (this._packedData & DependencyProperty.Flags.IsPotentiallyUsingDefaultValueFactory) != (DependencyProperty.Flags) 0;
          }
        }
    
        public bool ReadOnly
        {
          get
          {
            return this._readOnlyKey != null;
          }
        }
    
        internal CoerceValueCallback DesignerCoerceValueCallback
        {
          get
          {
            return this._designerCoerceValueCallback;
          }
          set
          {
            if (this.ReadOnly)
              throw new InvalidOperationException(System.Windows.SR.Get("ReadOnlyDesignerCoersionNotAllowed", new object[1]
              {
                (object) this.Name
              }));
            else
              this._designerCoerceValueCallback = value;
          }
        }
    
        internal static int RegisteredPropertyCount
        {
          get
          {
            return DependencyProperty.RegisteredPropertyList.Count;
          }
        }
    
        internal static IEnumerable RegisteredProperties
        {
          get
          {
            return (IEnumerable) new DependencyProperty.RegisteredPropertiesEnumerable(-2);
          }
        }
    
        static DependencyProperty()
        {
          DependencyProperty.UnsetValue = (object) new NamedObject("DependencyProperty.UnsetValue");
          DependencyProperty.RegisteredPropertyList = new ItemStructList<DependencyProperty>(768);
          DependencyProperty.PropertyFromName = new Hashtable();
          DependencyProperty.Synchronized = new object();
          DependencyProperty.NullableType = typeof (Nullable<>);
        }
    
        private DependencyProperty(string name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata, ValidateValueCallback validateValueCallback)
        {
          this._metadataMap = new InsertionSortMap();
          base.\u002Ector();
          this._name = name;
          this._propertyType = propertyType;
          this._ownerType = ownerType;
          this._defaultMetadata = defaultMetadata;
          this._validateValueCallback = validateValueCallback;
          object obj;
          Monitor.Enter(obj = DependencyProperty.Synchronized);
          DependencyProperty.Flags flags;
          try
          {
            flags = (DependencyProperty.Flags) DependencyProperty.GetUniqueGlobalIndex(ownerType, name);
            DependencyProperty.RegisteredPropertyList.Add(this);
          }
          finally
          {
            Monitor.Exit(obj);
          }
          if (propertyType.IsValueType)
            flags |= DependencyProperty.Flags.IsValueType;
          if (propertyType == typeof (object))
            flags |= DependencyProperty.Flags.IsObjectType;
          if (typeof (Freezable).IsAssignableFrom(propertyType))
            flags |= DependencyProperty.Flags.IsFreezableType;
          if (propertyType == typeof (string))
            flags |= DependencyProperty.Flags.IsStringType;
          this._packedData = flags;
        }
    
        public static DependencyProperty Register(string name, Type propertyType, Type ownerType)
        {
          return DependencyProperty.Register(name, propertyType, ownerType, (PropertyMetadata) null, (ValidateValueCallback) null);
        }
    
        public static DependencyProperty Register(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata)
        {
          return DependencyProperty.Register(name, propertyType, ownerType, typeMetadata, (ValidateValueCallback) null);
        }
    
        public static DependencyProperty Register(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata, ValidateValueCallback validateValueCallback)
        {
          DependencyProperty.RegisterParameterValidation(name, propertyType, ownerType);
          PropertyMetadata defaultMetadata = (PropertyMetadata) null;
          if (typeMetadata != null && typeMetadata.DefaultValueWasSet())
            defaultMetadata = new PropertyMetadata(typeMetadata.DefaultValue);
          DependencyProperty dependencyProperty = DependencyProperty.RegisterCommon(name, propertyType, ownerType, defaultMetadata, validateValueCallback);
          if (typeMetadata != null)
            dependencyProperty.OverrideMetadata(ownerType, typeMetadata);
          return dependencyProperty;
        }
    
        public static DependencyPropertyKey RegisterReadOnly(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata)
        {
          return DependencyProperty.RegisterReadOnly(name, propertyType, ownerType, typeMetadata, (ValidateValueCallback) null);
        }
    
        public static DependencyPropertyKey RegisterReadOnly(string name, Type propertyType, Type ownerType, PropertyMetadata typeMetadata, ValidateValueCallback validateValueCallback)
        {
          DependencyProperty.RegisterParameterValidation(name, propertyType, ownerType);
          PropertyMetadata defaultMetadata = typeMetadata == null || !typeMetadata.DefaultValueWasSet() ? DependencyProperty.AutoGeneratePropertyMetadata(propertyType, validateValueCallback, name, ownerType) : new PropertyMetadata(typeMetadata.DefaultValue);
          DependencyPropertyKey key = new DependencyPropertyKey((DependencyProperty) null);
          DependencyProperty dp = DependencyProperty.RegisterCommon(name, propertyType, ownerType, defaultMetadata, validateValueCallback);
          dp._readOnlyKey = key;
          key.SetDependencyProperty(dp);
          if (typeMetadata == null)
            typeMetadata = DependencyProperty.AutoGeneratePropertyMetadata(propertyType, validateValueCallback, name, ownerType);
          dp.OverrideMetadata(ownerType, typeMetadata, key);
          return key;
        }
    
        public static DependencyPropertyKey RegisterAttachedReadOnly(string name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata)
        {
          return DependencyProperty.RegisterAttachedReadOnly(name, propertyType, ownerType, defaultMetadata, (ValidateValueCallback) null);
        }
    
        public static DependencyPropertyKey RegisterAttachedReadOnly(string name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata, ValidateValueCallback validateValueCallback)
        {
          DependencyProperty.RegisterParameterValidation(name, propertyType, ownerType);
          if (defaultMetadata == null)
            defaultMetadata = DependencyProperty.AutoGeneratePropertyMetadata(propertyType, validateValueCallback, name, ownerType);
          DependencyPropertyKey dependencyPropertyKey = new DependencyPropertyKey((DependencyProperty) null);
          DependencyProperty dp = DependencyProperty.RegisterCommon(name, propertyType, ownerType, defaultMetadata, validateValueCallback);
          dp._readOnlyKey = dependencyPropertyKey;
          dependencyPropertyKey.SetDependencyProperty(dp);
          return dependencyPropertyKey;
        }
    
        public static DependencyProperty RegisterAttached(string name, Type propertyType, Type ownerType)
        {
          return DependencyProperty.RegisterAttached(name, propertyType, ownerType, (PropertyMetadata) null, (ValidateValueCallback) null);
        }
    
        public static DependencyProperty RegisterAttached(string name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata)
        {
          return DependencyProperty.RegisterAttached(name, propertyType, ownerType, defaultMetadata, (ValidateValueCallback) null);
        }
    
        public static DependencyProperty RegisterAttached(string name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata, ValidateValueCallback validateValueCallback)
        {
          DependencyProperty.RegisterParameterValidation(name, propertyType, ownerType);
          return DependencyProperty.RegisterCommon(name, propertyType, ownerType, defaultMetadata, validateValueCallback);
        }
    
        private static void RegisterParameterValidation(string name, Type propertyType, Type ownerType)
        {
          if (name == null)
            throw new ArgumentNullException("name");
          if (name.Length == 0)
            throw new ArgumentException(System.Windows.SR.Get("StringEmpty"), "name");
          if (ownerType == null)
            throw new ArgumentNullException("ownerType");
          if (propertyType == null)
            throw new ArgumentNullException("propertyType");
        }
    
        private static DependencyProperty RegisterCommon(string name, Type propertyType, Type ownerType, PropertyMetadata defaultMetadata, ValidateValueCallback validateValueCallback)
        {
          DependencyProperty.FromNameKey fromNameKey = new DependencyProperty.FromNameKey(name, ownerType);
          object obj1;
          Monitor.Enter(obj1 = DependencyProperty.Synchronized);
          try
          {
            if (DependencyProperty.PropertyFromName.Contains((object) fromNameKey))
              throw new ArgumentException(System.Windows.SR.Get("PropertyAlreadyRegistered", (object) name, (object) ownerType.Name));
          }
          finally
          {
            Monitor.Exit(obj1);
          }
          if (defaultMetadata == null)
          {
            defaultMetadata = DependencyProperty.AutoGeneratePropertyMetadata(propertyType, validateValueCallback, name, ownerType);
          }
          else
          {
            if (!defaultMetadata.DefaultValueWasSet())
              defaultMetadata.DefaultValue = DependencyProperty.AutoGenerateDefaultValue(propertyType);
            DependencyProperty.ValidateMetadataDefaultValue(defaultMetadata, propertyType, name, validateValueCallback);
          }
          DependencyProperty dp = new DependencyProperty(name, propertyType, ownerType, defaultMetadata, validateValueCallback);
          defaultMetadata.Seal(dp, (Type) null);
          if (defaultMetadata.IsInherited)
            dp._packedData |= DependencyProperty.Flags.IsPotentiallyInherited;
          if (defaultMetadata.UsingDefaultValueFactory)
            dp._packedData |= DependencyProperty.Flags.IsPotentiallyUsingDefaultValueFactory;
          object obj2;
          Monitor.Enter(obj2 = DependencyProperty.Synchronized);
          try
          {
            DependencyProperty.PropertyFromName[(object) fromNameKey] = (object) dp;
          }
          finally
          {
            Monitor.Exit(obj2);
          }
          if (TraceDependencyProperty.IsEnabled)
            TraceDependencyProperty.TraceActivityItem(TraceDependencyProperty.Register, (object) dp, (object) dp.OwnerType);
          return dp;
        }
    
        private static object AutoGenerateDefaultValue(Type propertyType)
        {
          object obj = (object) null;
          if (propertyType.IsValueType)
            obj = Activator.CreateInstance(propertyType);
          return obj;
        }
    
        private static PropertyMetadata AutoGeneratePropertyMetadata(Type propertyType, ValidateValueCallback validateValueCallback, string name, Type ownerType)
        {
          object defaultValue = DependencyProperty.AutoGenerateDefaultValue(propertyType);
          if (validateValueCallback == null || validateValueCallback(defaultValue))
            return new PropertyMetadata(defaultValue);
          throw new ArgumentException(System.Windows.SR.Get("DefaultValueAutoAssignFailed", (object) name, (object) ownerType.Name));
        }
    
        private static void ValidateMetadataDefaultValue(PropertyMetadata defaultMetadata, Type propertyType, string propertyName, ValidateValueCallback validateValueCallback)
        {
          if (defaultMetadata.UsingDefaultValueFactory)
            return;
          DependencyProperty.ValidateDefaultValueCommon(defaultMetadata.DefaultValue, propertyType, propertyName, validateValueCallback, true);
        }
    
        internal void ValidateFactoryDefaultValue(object defaultValue)
        {
          DependencyProperty.ValidateDefaultValueCommon(defaultValue, this.PropertyType, this.Name, this.ValidateValueCallback, false);
        }
    
        private static void ValidateDefaultValueCommon(object defaultValue, Type propertyType, string propertyName, ValidateValueCallback validateValueCallback, bool checkThreadAffinity)
        {
          if (!DependencyProperty.IsValidType(defaultValue, propertyType))
          {
            throw new ArgumentException(System.Windows.SR.Get("DefaultValuePropertyTypeMismatch", new object[1]
            {
              (object) propertyName
            }));
          }
          else
          {
            if (defaultValue is Expression)
              throw new ArgumentException(System.Windows.SR.Get("DefaultValueMayNotBeExpression"));
            if (checkThreadAffinity)
            {
              DispatcherObject dispatcherObject = defaultValue as DispatcherObject;
              if (dispatcherObject != null && dispatcherObject.Dispatcher != null)
              {
                ISealable sealable = dispatcherObject as ISealable;
                if (sealable != null && sealable.CanSeal)
                {
                  Invariant.Assert(!sealable.IsSealed, "A Sealed ISealable must not have dispatcher affinity");
                  sealable.Seal();
                  Invariant.Assert(dispatcherObject.Dispatcher == null, "ISealable.Seal() failed after ISealable.CanSeal returned true");
                }
                else
                  throw new ArgumentException(System.Windows.SR.Get("DefaultValueMustBeFreeThreaded", new object[1]
                  {
                    (object) propertyName
                  }));
              }
            }
            if (validateValueCallback == null || validateValueCallback(defaultValue))
              return;
            throw new ArgumentException(System.Windows.SR.Get("DefaultValueInvalid", new object[1]
            {
              (object) propertyName
            }));
          }
        }
    
        private void SetupOverrideMetadata(Type forType, PropertyMetadata typeMetadata, out DependencyObjectType dType, out PropertyMetadata baseMetadata)
        {
          if (forType == null)
            throw new ArgumentNullException("forType");
          if (typeMetadata == null)
            throw new ArgumentNullException("typeMetadata");
          if (typeMetadata.Sealed)
            throw new ArgumentException(System.Windows.SR.Get("TypeMetadataAlreadyInUse"));
          if (!typeof (DependencyObject).IsAssignableFrom(forType))
          {
            throw new ArgumentException(System.Windows.SR.Get("TypeMustBeDependencyObjectDerived", new object[1]
            {
              (object) forType.Name
            }));
          }
          else
          {
            if (typeMetadata.IsDefaultValueModified)
              DependencyProperty.ValidateMetadataDefaultValue(typeMetadata, this.PropertyType, this.Name, this.ValidateValueCallback);
            dType = DependencyObjectType.FromSystemType(forType);
            baseMetadata = this.GetMetadata(dType.BaseType);
            if (!baseMetadata.GetType().IsAssignableFrom(typeMetadata.GetType()))
              throw new ArgumentException(System.Windows.SR.Get("OverridingMetadataDoesNotMatchBaseMetadataType"));
          }
        }
    
        public void OverrideMetadata(Type forType, PropertyMetadata typeMetadata)
        {
          DependencyObjectType dType;
          PropertyMetadata baseMetadata;
          this.SetupOverrideMetadata(forType, typeMetadata, out dType, out baseMetadata);
          if (this.ReadOnly)
            throw new InvalidOperationException(System.Windows.SR.Get("ReadOnlyOverrideNotAllowed", new object[1]
            {
              (object) this.Name
            }));
          else
            this.ProcessOverrideMetadata(forType, typeMetadata, dType, baseMetadata);
        }
    
        public void OverrideMetadata(Type forType, PropertyMetadata typeMetadata, DependencyPropertyKey key)
        {
          DependencyObjectType dType;
          PropertyMetadata baseMetadata;
          this.SetupOverrideMetadata(forType, typeMetadata, out dType, out baseMetadata);
          if (key == null)
            throw new ArgumentNullException("key");
          if (!this.ReadOnly)
            throw new InvalidOperationException(System.Windows.SR.Get("PropertyNotReadOnly"));
          if (key.DependencyProperty != this)
          {
            throw new ArgumentException(System.Windows.SR.Get("ReadOnlyOverrideKeyNotAuthorized", new object[1]
            {
              (object) this.Name
            }));
          }
          else
          {
            this.VerifyReadOnlyKey(key);
            this.ProcessOverrideMetadata(forType, typeMetadata, dType, baseMetadata);
          }
        }
    
        private void ProcessOverrideMetadata(Type forType, PropertyMetadata typeMetadata, DependencyObjectType dType, PropertyMetadata baseMetadata)
        {
          object obj;
          Monitor.Enter(obj = DependencyProperty.Synchronized);
          try
          {
            if (DependencyProperty.UnsetValue == this._metadataMap[dType.Id])
              this._metadataMap[dType.Id] = (object) typeMetadata;
            else
              throw new ArgumentException(System.Windows.SR.Get("TypeMetadataAlreadyRegistered", new object[1]
              {
                (object) forType.Name
              }));
          }
          finally
          {
            Monitor.Exit(obj);
          }
          typeMetadata.InvokeMerge(baseMetadata, this);
          typeMetadata.Seal(this, forType);
          if (typeMetadata.IsInherited)
            this._packedData |= DependencyProperty.Flags.IsPotentiallyInherited;
          if (typeMetadata.DefaultValueWasSet() && typeMetadata.DefaultValue != this.DefaultMetadata.DefaultValue)
            this._packedData |= DependencyProperty.Flags.IsDefaultValueChanged;
          if (!typeMetadata.UsingDefaultValueFactory)
            return;
          this._packedData |= DependencyProperty.Flags.IsPotentiallyUsingDefaultValueFactory;
        }
    
        [FriendAccessAllowed]
        internal object GetDefaultValue(DependencyObjectType dependencyObjectType)
        {
          if (!this.IsDefaultValueChanged)
            return this.DefaultMetadata.DefaultValue;
          else
            return this.GetMetadata(dependencyObjectType).DefaultValue;
        }
    
        [FriendAccessAllowed]
        internal object GetDefaultValue(Type forType)
        {
          if (!this.IsDefaultValueChanged)
            return this.DefaultMetadata.DefaultValue;
          else
            return this.GetMetadata(DependencyObjectType.FromSystemTypeInternal(forType)).DefaultValue;
        }
    
        public PropertyMetadata GetMetadata(Type forType)
        {
          if (forType != null)
            return this.GetMetadata(DependencyObjectType.FromSystemType(forType));
          else
            throw new ArgumentNullException("forType");
        }
    
        public PropertyMetadata GetMetadata(DependencyObject dependencyObject)
        {
          if (dependencyObject != null)
            return this.GetMetadata(dependencyObject.DependencyObjectType);
          else
            throw new ArgumentNullException("dependencyObject");
        }
    
        public PropertyMetadata GetMetadata(DependencyObjectType dependencyObjectType)
        {
          if (dependencyObjectType != null)
          {
            int index = this._metadataMap.Count - 1;
            if (index < 0)
              return this._defaultMetadata;
            if (index == 0)
            {
              int key;
              object obj;
              this._metadataMap.GetKeyValuePair(index, out key, out obj);
              while (dependencyObjectType.Id > key)
                dependencyObjectType = dependencyObjectType.BaseType;
              if (key == dependencyObjectType.Id)
                return (PropertyMetadata) obj;
            }
            else if (dependencyObjectType.Id != 0)
            {
              do
              {
                int key;
                object obj;
                this._metadataMap.GetKeyValuePair(index, out key, out obj);
                for (--index; dependencyObjectType.Id < key && index >= 0; --index)
                  this._metadataMap.GetKeyValuePair(index, out key, out obj);
                while (dependencyObjectType.Id > key)
                  dependencyObjectType = dependencyObjectType.BaseType;
                if (key == dependencyObjectType.Id)
                  return (PropertyMetadata) obj;
              }
              while (index >= 0);
            }
          }
          return this._defaultMetadata;
        }
    
        public DependencyProperty AddOwner(Type ownerType)
        {
          return this.AddOwner(ownerType, (PropertyMetadata) null);
        }
    
        public DependencyProperty AddOwner(Type ownerType, PropertyMetadata typeMetadata)
        {
          if (ownerType == null)
            throw new ArgumentNullException("ownerType");
          DependencyProperty.FromNameKey fromNameKey = new DependencyProperty.FromNameKey(this.Name, ownerType);
          object obj1;
          Monitor.Enter(obj1 = DependencyProperty.Synchronized);
          try
          {
            if (DependencyProperty.PropertyFromName.Contains((object) fromNameKey))
              throw new ArgumentException(System.Windows.SR.Get("PropertyAlreadyRegistered", (object) this.Name, (object) ownerType.Name));
          }
          finally
          {
            Monitor.Exit(obj1);
          }
          if (typeMetadata != null)
            this.OverrideMetadata(ownerType, typeMetadata);
          object obj2;
          Monitor.Enter(obj2 = DependencyProperty.Synchronized);
          try
          {
            DependencyProperty.PropertyFromName[(object) fromNameKey] = (object) this;
          }
          finally
          {
            Monitor.Exit(obj2);
          }
          return this;
        }
    
        public override int GetHashCode()
        {
          return this.GlobalIndex;
        }
    
        public bool IsValidType(object value)
        {
          return DependencyProperty.IsValidType(value, this.PropertyType);
        }
    
        public bool IsValidValue(object value)
        {
          if (!DependencyProperty.IsValidType(value, this.PropertyType))
            return false;
          if (this.ValidateValueCallback != null)
            return this.ValidateValueCallback(value);
          else
            return true;
        }
    
        internal void VerifyReadOnlyKey(DependencyPropertyKey candidateKey)
        {
          if (this._readOnlyKey != candidateKey)
            throw new ArgumentException(System.Windows.SR.Get("ReadOnlyKeyNotAuthorized"));
        }
    
        internal bool IsValidValueInternal(object value)
        {
          if (this.ValidateValueCallback != null)
            return this.ValidateValueCallback(value);
          else
            return true;
        }
    
        [FriendAccessAllowed]
        internal static DependencyProperty FromName(string name, Type ownerType)
        {
          DependencyProperty dependencyProperty = (DependencyProperty) null;
          if (name == null)
            throw new ArgumentNullException("name");
          if (ownerType == null)
            throw new ArgumentNullException("ownerType");
          DependencyProperty.FromNameKey fromNameKey = new DependencyProperty.FromNameKey(name, ownerType);
          for (; dependencyProperty == null && ownerType != null; ownerType = ownerType.BaseType)
          {
            RuntimeHelpers.RunClassConstructor(ownerType.TypeHandle);
            fromNameKey.UpdateNameKey(ownerType);
            object obj;
            Monitor.Enter(obj = DependencyProperty.Synchronized);
            try
            {
              dependencyProperty = (DependencyProperty) DependencyProperty.PropertyFromName[(object) fromNameKey];
            }
            finally
            {
              Monitor.Exit(obj);
            }
          }
          return dependencyProperty;
        }
    
        public override string ToString()
        {
          return this._name;
        }
    
        internal static bool IsValidType(object value, Type propertyType)
        {
          if (value == null)
          {
            if (propertyType.IsValueType && (!propertyType.IsGenericType || propertyType.GetGenericTypeDefinition() != DependencyProperty.NullableType))
              return false;
          }
          else if (!propertyType.IsInstanceOfType(value))
            return false;
          return true;
        }
    
        internal static int GetUniqueGlobalIndex(Type ownerType, string name)
        {
          if (DependencyProperty.GlobalIndexCount < (int) ushort.MaxValue)
            return DependencyProperty.GlobalIndexCount++;
          if (ownerType != null)
            throw new InvalidOperationException(System.Windows.SR.Get("TooManyDependencyProperties", new object[1]
            {
              (object) (ownerType.Name + "." + name)
            }));
          else
            throw new InvalidOperationException(System.Windows.SR.Get("TooManyDependencyProperties", new object[1]
            {
              (object) "ConstantProperty"
            }));
        }
    
        private class FromNameKey
        {
          private string _name;
          private Type _ownerType;
          private int _hashCode;
    
          public FromNameKey(string name, Type ownerType)
          {
            base.\u002Ector();
            this._name = name;
            this._ownerType = ownerType;
            this._hashCode = this._name.GetHashCode() ^ this._ownerType.GetHashCode();
          }
    
          public void UpdateNameKey(Type ownerType)
          {
            this._ownerType = ownerType;
            this._hashCode = this._name.GetHashCode() ^ this._ownerType.GetHashCode();
          }
    
          public override int GetHashCode()
          {
            return this._hashCode;
          }
    
          public override bool Equals(object o)
          {
            if (o != null && o is DependencyProperty.FromNameKey)
              return this.Equals((DependencyProperty.FromNameKey) o);
            else
              return false;
          }
    
          public bool Equals(DependencyProperty.FromNameKey key)
          {
            if (this._name.Equals(key._name))
              return this._ownerType == key._ownerType;
            else
              return false;
          }
        }
    
        [System.Flags]
        private enum Flags
        {
          GlobalIndexMask = 65535,
          IsValueType = 65536,
          IsFreezableType = 131072,
          IsStringType = 262144,
          IsPotentiallyInherited = 524288,
          IsDefaultValueChanged = 1048576,
          IsPotentiallyUsingDefaultValueFactory = 2097152,
          IsObjectType = 4194304,
        }
    
        private sealed class RegisteredPropertiesEnumerable : IEnumerable<object>, IEnumerable, IEnumerator<object>, IEnumerator, IDisposable
        {
          private int _state;
          private object _current;
          private int _index;
          private int _threadId;
          private DependencyProperty[] _propertyList;
    
          object IEnumerator<object>.Current
          {
            get
            {
              return this._current;
            }
          }
    
          object IEnumerator.Current
          {
            get
            {
              return this._current;
            }
          }
    
          public RegisteredPropertiesEnumerable(int state)
          {
            base.\u002Ector();
            this._state = state;
            this._threadId = Thread.CurrentThread.ManagedThreadId;
          }
    
          IEnumerator<object> IEnumerable<object>.GetEnumerator()
          {
            if (Thread.CurrentThread.ManagedThreadId != this._threadId || this._state != -2)
              return (IEnumerator<object>) new DependencyProperty.RegisteredPropertiesEnumerable(0);
            this._state = 0;
            return (IEnumerator<object>) this;
          }
    
          IEnumerator IEnumerable.GetEnumerator()
          {
            return (IEnumerator) this.GetEnumerator();
          }
    
          void IEnumerator.Reset()
          {
            throw new NotSupportedException();
          }
    
          public bool MoveNext()
          {
            bool flag = true;
            try
            {
              switch (this._state)
              {
                case 0:
                  this._state = 1;
                  this._propertyList = DependencyProperty.RegisteredPropertyList.List;
                  this._index = 0;
                  break;
                case 2:
                  this._state = 1;
                  ++this._index;
                  break;
                default:
                  flag = false;
                  return false;
              }
              for (; this._index < this._propertyList.Length; ++this._index)
              {
                DependencyProperty dependencyProperty = this._propertyList[this._index];
                if (dependencyProperty != null)
                {
                  this._current = (object) dependencyProperty;
                  this._state = 2;
                  flag = false;
                  return true;
                }
              }
              this.Finally2();
              flag = false;
              return false;
            }
            finally
            {
              if (flag)
                this.Dispose();
            }
          }
    
          void IDisposable.Dispose()
          {
            switch (this._state)
            {
              case 1:
              case 2:
                this.Finally2();
                break;
            }
          }
    
          private void Finally2()
          {
            this._state = -1;
          }
        }
      }
    }

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月8日 2:11
    版主
  • 非常感谢!O(∩_∩)O~

    此去千万里,岁月忽已晚!

    2013年6月8日 6:40