//------------------------------------------------------------------------------ // // Copyright (c) Microsoft Corporation. All rights reserved. // //------------------------------------------------------------------------------ /* */ [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2113:SecureLateBindingMethods", Scope="member", Target="System.ComponentModel.PropertyDescriptor.GetTypeFromName(System.String):System.Type")] namespace System.ComponentModel { using Microsoft.Win32; using System; using System.Collections; using System.ComponentModel.Design; using System.Diagnostics; using System.Reflection; using System.Runtime.InteropServices; using System.Runtime.Remoting.Activation; using System.Runtime.Serialization.Formatters; using System.Security; using System.Security.Permissions; /// /// Provides a description of a property. /// [HostProtection(SharedState = true)] [System.Runtime.InteropServices.ComVisible(true)] public abstract class PropertyDescriptor : MemberDescriptor { private TypeConverter converter = null; private Hashtable valueChangedHandlers; private object[] editors; private Type[] editorTypes; private int editorCount; /// /// /// Initializes a new instance of the class with the specified name and /// attributes. /// /// protected PropertyDescriptor(string name, Attribute[] attrs) : base(name, attrs) { } /// /// /// Initializes a new instance of the class with /// the name and attributes in the specified . /// /// protected PropertyDescriptor(MemberDescriptor descr) : base(descr) { } /// /// /// Initializes a new instance of the class with /// the name in the specified and the /// attributes in both the and the /// array. /// /// protected PropertyDescriptor(MemberDescriptor descr, Attribute[] attrs) : base(descr, attrs) { } /// /// /// When overridden in a derived class, gets the type of the /// component this property /// is bound to. /// /// public abstract Type ComponentType {get;} /// /// /// Gets the type converter for this property. /// /// public virtual TypeConverter Converter { get { // Always grab the attribute collection first here, because if the metadata version // changes it will invalidate our type converter cache. AttributeCollection attrs = Attributes; if (converter == null) { TypeConverterAttribute attr = (TypeConverterAttribute)attrs[typeof(TypeConverterAttribute)]; if (attr.ConverterTypeName != null && attr.ConverterTypeName.Length > 0) { Type converterType = GetTypeFromName(attr.ConverterTypeName); if (converterType != null && typeof(TypeConverter).IsAssignableFrom(converterType)) { converter = (TypeConverter)CreateInstance(converterType); } } if (converter == null) { converter = TypeDescriptor.GetConverter(PropertyType); } } return converter; } } /// /// /// Gets a value /// indicating whether this property should be localized, as /// specified in the . /// /// public virtual bool IsLocalizable { get { return(LocalizableAttribute.Yes.Equals(Attributes[typeof(LocalizableAttribute)])); } } /// /// /// When overridden in /// a derived class, gets a value /// indicating whether this property is read-only. /// /// public abstract bool IsReadOnly { get;} /// /// /// Gets a value /// indicating whether this property should be serialized as specified in the . /// /// public DesignerSerializationVisibility SerializationVisibility { get { DesignerSerializationVisibilityAttribute attr = (DesignerSerializationVisibilityAttribute)Attributes[typeof(DesignerSerializationVisibilityAttribute)]; return attr.Visibility; } } /// /// /// When overridden in a derived class, /// gets the type of the property. /// /// public abstract Type PropertyType { get;} /// /// Allows interested objects to be notified when this property changes. /// public virtual void AddValueChanged(object component, EventHandler handler) { if (component == null) throw new ArgumentNullException("component"); if (handler == null) throw new ArgumentNullException("handler"); if (valueChangedHandlers == null) { valueChangedHandlers = new Hashtable(); } EventHandler h = (EventHandler)valueChangedHandlers[component]; valueChangedHandlers[component] = Delegate.Combine(h, handler); } /// /// /// When overridden in a derived class, indicates whether /// resetting the will change the value of the /// . /// /// public abstract bool CanResetValue(object component); /// /// /// Compares this to another /// to see if they are equivalent. /// NOTE: If you make a change here, you likely need to change GetHashCode() as well. /// /// public override bool Equals(object obj) { try { if (obj == this) { return true; } if (obj == null) { return false; } // Assume that 90% of the time we will only do a .Equals(...) for // propertydescriptor vs. propertydescriptor... avoid the overhead // of an instanceof call. PropertyDescriptor pd = obj as PropertyDescriptor; if (pd != null && pd.NameHashCode == this.NameHashCode && pd.PropertyType == this.PropertyType && pd.Name.Equals(this.Name)) { return true; } } catch {} return false; } /// /// /// Creates an instance of the /// specified type. /// /// protected object CreateInstance(Type type) { Type[] typeArgs = new Type[] {typeof(Type)}; ConstructorInfo ctor = type.GetConstructor(typeArgs); if (ctor != null) { return TypeDescriptor.CreateInstance(null, type, typeArgs, new object[] {PropertyType}); } return TypeDescriptor.CreateInstance(null, type, null, null); } /// /// In an inheriting class, adds the attributes of the inheriting class to the /// specified list of attributes in the parent class. For duplicate attributes, /// the last one added to the list will be kept. /// protected override void FillAttributes(IList attributeList) { // Each time we fill our attributes, we should clear our cached // stuff. converter = null; editors = null; editorTypes = null; editorCount = 0; base.FillAttributes(attributeList); } /// /// /// [To be supplied.] /// public PropertyDescriptorCollection GetChildProperties() { return GetChildProperties(null, null); } /// /// [To be supplied.] /// public PropertyDescriptorCollection GetChildProperties(Attribute[] filter) { return GetChildProperties(null, filter); } /// /// [To be supplied.] /// public PropertyDescriptorCollection GetChildProperties(object instance) { return GetChildProperties(instance, null); } /// /// Retrieves the properties /// public virtual PropertyDescriptorCollection GetChildProperties(object instance, Attribute[] filter) { if (instance == null) { return TypeDescriptor.GetProperties(PropertyType, filter); } else { return TypeDescriptor.GetProperties(instance, filter); } } /// /// /// Gets an editor of the specified type. /// /// public virtual object GetEditor(Type editorBaseType) { object editor = null; // Always grab the attribute collection first here, because if the metadata version // changes it will invalidate our editor cache. AttributeCollection attrs = Attributes; // Check the editors we've already created for this type. // if (editorTypes != null) { for (int i = 0; i < editorCount; i++) { if (editorTypes[i] == editorBaseType) { return editors[i]; } } } // If one wasn't found, then we must go through the attributes. // if (editor == null) { for (int i = 0; i < attrs.Count; i++) { EditorAttribute attr = attrs[i] as EditorAttribute; if (attr == null) { continue; } Type editorType = GetTypeFromName(attr.EditorBaseTypeName); if (editorBaseType == editorType) { Type type = GetTypeFromName(attr.EditorTypeName); if (type != null) { editor = CreateInstance(type); break; } } } // Now, if we failed to find it in our own attributes, go to the // component descriptor. // if (editor == null) { editor = TypeDescriptor.GetEditor(PropertyType, editorBaseType); } // Now, another slot in our editor cache for next time // if (editorTypes == null) { editorTypes = new Type[5]; editors = new object[5]; } if (editorCount >= editorTypes.Length) { Type[] newTypes = new Type[editorTypes.Length * 2]; object[] newEditors = new object[editors.Length * 2]; Array.Copy(editorTypes, newTypes, editorTypes.Length); Array.Copy(editors, newEditors, editors.Length); editorTypes = newTypes; editors = newEditors; } editorTypes[editorCount] = editorBaseType; editors[editorCount++] = editor; } return editor; } /// /// Try to keep this reasonable in [....] with Equals(). Specifically, /// if A.Equals(B) returns true, A & B should have the same hash code. /// public override int GetHashCode() { return this.NameHashCode ^ PropertyType.GetHashCode(); } /// /// This method returns the object that should be used during invocation of members. /// Normally the return value will be the same as the instance passed in. If /// someone associated another object with this instance, or if the instance is a /// custom type descriptor, GetInvocationTarget may return a different value. /// protected override object GetInvocationTarget(Type type, object instance) { object target = base.GetInvocationTarget(type, instance); ICustomTypeDescriptor td = target as ICustomTypeDescriptor; if (td != null) { target = td.GetPropertyOwner(this); } return target; } /// /// Gets a type using its name. /// protected Type GetTypeFromName(string typeName) { if (typeName == null || typeName.Length == 0) { return null; } // try the generic method. Type typeFromGetType = Type.GetType(typeName); // If we didn't get a type from the generic method, or if the assembly we found the type // in is the same as our Component's assembly, use or Component's assembly instead. This is // because the CLR may have cached an older version if the assembly's version number didn't change // See VSWhidbey 560732 Type typeFromComponent = null; if (ComponentType != null) { if ((typeFromGetType == null) || (ComponentType.Assembly.FullName.Equals(typeFromGetType.Assembly.FullName))) { int comma = typeName.IndexOf(','); if (comma != -1) typeName = typeName.Substring(0, comma); typeFromComponent = ComponentType.Assembly.GetType(typeName); } } return typeFromComponent ?? typeFromGetType; } /// /// /// When overridden in a derived class, gets the current /// value /// of the /// property on a component. /// /// public abstract object GetValue(object component); /// /// This should be called by your property descriptor implementation /// when the property value has changed. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers")] protected virtual void OnValueChanged(object component, EventArgs e) { if (component != null && valueChangedHandlers != null) { EventHandler handler = (EventHandler)valueChangedHandlers[component]; if (handler != null) { handler(component, e); } } } /// /// Allows interested objects to be notified when this property changes. /// public virtual void RemoveValueChanged(object component, EventHandler handler) { if (component == null) throw new ArgumentNullException("component"); if (handler == null) throw new ArgumentNullException("handler"); if (valueChangedHandlers != null) { EventHandler h = (EventHandler)valueChangedHandlers[component]; h = (EventHandler)Delegate.Remove(h, handler); if (h != null) { valueChangedHandlers[component] = h; } else { valueChangedHandlers.Remove(component); } } } /// /// Return current set of ValueChanged event handlers for a specific /// component, in the form of a combined multicast event handler. /// Returns null if no event handlers currently assigned to component. /// internal protected EventHandler GetValueChangedHandler(object component) { if (component != null && valueChangedHandlers != null) { return (EventHandler) valueChangedHandlers[component]; } else { return null; } } /// /// /// When overridden in a derived class, resets the /// value /// for this property /// of the component. /// /// public abstract void ResetValue(object component); /// /// /// When overridden in a derived class, sets the value of /// the component to a different value. /// /// public abstract void SetValue(object component, object value); /// /// /// When overridden in a derived class, indicates whether the /// value of /// this property needs to be persisted. /// /// public abstract bool ShouldSerializeValue(object component); /// /// Indicates whether value change notifications for this property may originate from outside the property /// descriptor, such as from the component itself (value=true), or whether notifications will only originate /// from direct calls made to PropertyDescriptor.SetValue (value=false). For example, the component may /// implement the INotifyPropertyChanged interface, or may have an explicit '{name}Changed' event for this property. /// public virtual bool SupportsChangeEvents { get { return false; } } } }