示例#1
0
 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(SR.Get("TypeMetadataAlreadyInUse"));
     }
     if (!typeof(DependencyObject).IsAssignableFrom(forType))
     {
         throw new ArgumentException(SR.Get("TypeMustBeDependencyObjectDerived", forType.Name));
     }
     if (typeMetadata.IsDefaultValueModified)
     {
         ValidateMetadataDefaultValue(typeMetadata, PropertyType, Name, ValidateValueCallback);
     }
     dType        = DependencyObjectType.FromSystemType(forType);
     baseMetadata = GetMetadata(dType.BaseType);
     if (!baseMetadata.GetType().IsAssignableFrom(typeMetadata.GetType()))
     {
         throw new ArgumentException(SR.Get("OverridingMetadataDoesNotMatchBaseMetadataType"));
     }
 }
示例#2
0
 /// <summary>Returns the metadata for this dependency property as it exists on a specified existing type. </summary>
 /// <returns>A property metadata object.</returns>
 /// <param name="forType">The specific type from which to retrieve the dependency property metadata.</param>
 public PropertyMetadata GetMetadata(Type forType)
 {
     if (forType != null)
     {
         return(GetMetadata(DependencyObjectType.FromSystemType(forType)));
     }
     throw new ArgumentNullException("forType");
 }
示例#3
0
        /// <summary>
        ///     Parameter validation for OverrideMetadata, includes code to force
        /// all base classes of "forType" to register their metadata so we know
        /// what we are overriding.
        /// </summary>
        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(SR.TypeMetadataAlreadyInUse);
            }

            if (!typeof(DependencyObject).IsAssignableFrom(forType))
            {
                throw new ArgumentException(SR.Format(SR.TypeMustBeDependencyObjectDerived, forType.Name));
            }

            // Ensure default value is a correct value (if it was supplied,
            // otherwise, the default value will be taken from the base metadata
            // which was already validated)
            if (typeMetadata.IsDefaultValueModified)
            {
                // Will throw ArgumentException if fails.
                ValidateMetadataDefaultValue(typeMetadata, PropertyType, Name, ValidateValueCallback);
            }

            // Force all base classes to register their metadata
            dType = DependencyObjectType.FromSystemType(forType);

            // Get metadata for the base type
            baseMetadata = GetMetadata(dType.BaseType);

            // Make sure overriding metadata is the same type or derived type of
            // the base metadata
            if (!baseMetadata.GetType().IsAssignableFrom(typeMetadata.GetType()))
            {
                throw new ArgumentException(SR.OverridingMetadataDoesNotMatchBaseMetadataType);
            }
        }
        //------------------------------------------------------
        //
        //  Internal Methods
        //
        //------------------------------------------------------


        /// <summary>
        ///     Returns true if the dependency object passed to the method is a type,
        ///     subtype or implememts the interface of any of the the types contained
        ///     in this object.
        /// </summary>
        internal override bool IsBrowsable(DependencyObject d, DependencyProperty dp)
        {
            if (d == null)
            {
                throw new ArgumentNullException("d");
            }
            if (dp == null)
            {
                throw new ArgumentNullException("dp");
            }

            // Get the dependency object type for our target type.
            // We cannot assume the user didn't do something wrong and
            // feed us a type that is not a dependency object, but that is
            // rare enough that it is worth the try/catch here rather than
            // a double IsAssignableFrom (one here, and one in DependencyObjectType).
            // We still use a flag here rather than checking for a null
            // _dTargetType so that a bad property that throws won't consistently
            // slow the system down with ArgumentExceptions.

            if (!_dTargetTypeChecked)
            {
                try
                {
                    _dTargetType = DependencyObjectType.FromSystemType(_targetType);
                }
                catch (ArgumentException)
                {
                }

                _dTargetTypeChecked = true;
            }


            if (_dTargetType != null && _dTargetType.IsInstanceOfType(d))
            {
                return(true);
            }

            return(false);
        }