Code:
/ DotNET / DotNET / 8.0 / untmp / whidbey / REDBITS / ndp / fx / src / CompMod / System / ComponentModel / PropertyDescriptor.cs / 2 / PropertyDescriptor.cs
//------------------------------------------------------------------------------ //// 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; ////// [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; ///Provides a description of a property. ////// protected PropertyDescriptor(string name, Attribute[] attrs) : base(name, attrs) { } ////// Initializes a new instance of the ///class with the specified name and /// attributes. /// /// protected PropertyDescriptor(MemberDescriptor descr) : base(descr) { } ////// Initializes a new instance of the ///class with /// the name and attributes in the specified . /// /// protected PropertyDescriptor(MemberDescriptor descr, Attribute[] attrs) : base(descr, attrs) { } ////// Initializes a new instance of the ///class with /// the name in the specified and the /// attributes in both the and the /// array. /// /// public abstract Type ComponentType {get;} ////// When overridden in a derived class, gets the type of the /// component this property /// is bound to. /// ////// 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 the type converter for this property. /// ////// public virtual bool IsLocalizable { get { return(LocalizableAttribute.Yes.Equals(Attributes[typeof(LocalizableAttribute)])); } } ////// Gets a value /// indicating whether this property should be localized, as /// specified in the ///. /// /// public abstract bool IsReadOnly { get;} ////// When overridden in /// a derived class, gets a value /// indicating whether this property is read-only. /// ////// public DesignerSerializationVisibility SerializationVisibility { get { DesignerSerializationVisibilityAttribute attr = (DesignerSerializationVisibilityAttribute)Attributes[typeof(DesignerSerializationVisibilityAttribute)]; return attr.Visibility; } } ////// Gets a value /// indicating whether this property should be serialized as specified in the ///. /// /// public abstract Type PropertyType { get;} ////// When overridden in a derived class, /// gets the type of the property. /// ////// 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); } ////// public abstract bool CanResetValue(object component); ////// When overridden in a derived class, indicates whether /// resetting the ///will change the value of the /// . /// /// 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; } ////// 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. /// /// 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); } ////// Creates an instance of the /// specified type. /// ////// 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); } ////// /// 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); } ///[To be supplied.] ////// 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); } } ////// 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; } ////// Gets an editor of the specified type. /// ////// Try to keep this reasonable in sync 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; } ////// 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; } ///Gets a type using its name. ////// public abstract object GetValue(object component); ////// When overridden in a derived class, gets the current /// value /// of the /// property on a 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; } } ////// public abstract void ResetValue(object component); ////// When overridden in a derived class, resets the /// value /// for this property /// of the component. /// ////// public abstract void SetValue(object component, object value); ////// When overridden in a derived class, sets the value of /// the component to a different value. /// ////// public abstract bool ShouldSerializeValue(object component); ////// When overridden in a derived class, indicates whether the /// value of /// this property needs to be persisted. /// ////// 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; } } } }
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- XmlAtomicValue.cs
- _SslSessionsCache.cs
- CompilationPass2TaskInternal.cs
- SessionIDManager.cs
- Number.cs
- HtmlTable.cs
- SliderAutomationPeer.cs
- FormattedText.cs
- DetailsViewInsertedEventArgs.cs
- HttpStreamFormatter.cs
- AnnotationDocumentPaginator.cs
- ManagementInstaller.cs
- KeyPressEvent.cs
- UIElement.cs
- View.cs
- URLMembershipCondition.cs
- PopupControlService.cs
- DisplayInformation.cs
- Control.cs
- Membership.cs
- NumberFormatInfo.cs
- WinFormsComponentEditor.cs
- ByteStreamBufferedMessageData.cs
- LingerOption.cs
- AttachedPropertyMethodSelector.cs
- CorrelationManager.cs
- CursorEditor.cs
- TrailingSpaceComparer.cs
- TemplatedMailWebEventProvider.cs
- EncodingInfo.cs
- XmlReaderDelegator.cs
- DefinitionUpdate.cs
- ObjectDataSourceSelectingEventArgs.cs
- PreviewControlDesigner.cs
- COM2IManagedPerPropertyBrowsingHandler.cs
- ConfigXmlAttribute.cs
- BeginStoryboard.cs
- DispatchWrapper.cs
- XamlFilter.cs
- DesigntimeLicenseContextSerializer.cs
- _SpnDictionary.cs
- FaultDescriptionCollection.cs
- ParameterCollection.cs
- objectquery_tresulttype.cs
- DataGridViewColumn.cs
- WebAdminConfigurationHelper.cs
- SqlDataReaderSmi.cs
- XmlSchemaAnnotated.cs
- RegionIterator.cs
- ParameterModifier.cs
- SoapDocumentMethodAttribute.cs
- PropertyInformationCollection.cs
- SecurityDocument.cs
- ContentFilePart.cs
- QilDataSource.cs
- ByteStack.cs
- TriggerActionCollection.cs
- FrameworkReadOnlyPropertyMetadata.cs
- ComponentDispatcher.cs
- HtmlLabelAdapter.cs
- PositiveTimeSpanValidatorAttribute.cs
- CheckPair.cs
- ListViewItemMouseHoverEvent.cs
- HelpEvent.cs
- SplayTreeNode.cs
- ApplicationContext.cs
- TransformValueSerializer.cs
- ZoneButton.cs
- DataGridViewRowsRemovedEventArgs.cs
- WizardPanelChangingEventArgs.cs
- ExtensionElementCollection.cs
- FormViewUpdateEventArgs.cs
- RijndaelManaged.cs
- ExceptionUtil.cs
- TagMapInfo.cs
- InkCanvasAutomationPeer.cs
- localization.cs
- WaitForChangedResult.cs
- PrivateFontCollection.cs
- SoapSchemaExporter.cs
- DynamicValueConverter.cs
- PrintDialog.cs
- GridSplitterAutomationPeer.cs
- PathFigureCollection.cs
- ObjectStateEntryBaseUpdatableDataRecord.cs
- ControlBindingsCollection.cs
- JsonStringDataContract.cs
- BooleanAnimationBase.cs
- MediaContextNotificationWindow.cs
- PropertyConverter.cs
- WebPartConnectionsDisconnectVerb.cs
- ImageButton.cs
- FormViewCommandEventArgs.cs
- LambdaExpression.cs
- DataChangedEventManager.cs
- BufferBuilder.cs
- WeakHashtable.cs
- BaseParser.cs
- QuaternionAnimation.cs
- WebMessageBodyStyleHelper.cs