The Labs \ Source Viewer \ SSCLI \ System.ComponentModel \ ExtendedPropertyDescriptor

  1. //------------------------------------------------------------------------------
  2. // <copyright file="ExtendedPropertyDescriptor.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. /*
  16. */
  17. namespace System.ComponentModel
  18. {
  19.    
  20.     using Microsoft.Win32;
  21.     using System;
  22.     using System.Collections;
  23.     using System.ComponentModel.Design;
  24.     using System.Diagnostics;
  25.     using System.Security.Permissions;
  26.    
  27.     /// <internalonly/>
  28.     /// <devdoc>
  29.     /// <para>
  30.     /// This class wraps an PropertyDescriptor with something that looks like a property. It
  31.     /// allows you to treat extended properties the same as regular properties.
  32.     /// </para>
  33.     /// </devdoc>
  34.     [HostProtection(SharedState = true)]
  35.     internal sealed class ExtendedPropertyDescriptor : PropertyDescriptor
  36.     {
  37.        
  38.         private readonly ReflectPropertyDescriptor extenderInfo;
  39.         // the extender property
  40.         private readonly IExtenderProvider provider;
  41.         // the guy providing it
  42.         /// <devdoc>
  43.         /// Creates a new extended property info. Callers can then treat this as
  44.         /// a standard property.
  45.         /// </devdoc>
  46.         public ExtendedPropertyDescriptor(ReflectPropertyDescriptor extenderInfo, Type receiverType, IExtenderProvider provider, Attribute[] attributes) : base(extenderInfo, attributes)
  47.         {
  48.            
  49.             Debug.Assert(extenderInfo != null, "ExtendedPropertyDescriptor must have extenderInfo");
  50.             Debug.Assert(provider != null, "ExtendedPropertyDescriptor must have provider");
  51.            
  52.             ArrayList attrList = new ArrayList(AttributeArray);
  53.             attrList.Add(ExtenderProvidedPropertyAttribute.Create(extenderInfo, receiverType, provider));
  54.             if (extenderInfo.IsReadOnly) {
  55.                 attrList.Add(ReadOnlyAttribute.Yes);
  56.             }
  57.            
  58.             Attribute[] temp = new Attribute[attrList.Count];
  59.             attrList.CopyTo(temp, 0);
  60.             AttributeArray = temp;
  61.            
  62.             this.extenderInfo = extenderInfo;
  63.             this.provider = provider;
  64.         }
  65.        
  66.         public ExtendedPropertyDescriptor(PropertyDescriptor extender, Attribute[] attributes) : base(extender, attributes)
  67.         {
  68.             Debug.Assert(extender != null, "The original PropertyDescriptor must be non-null");
  69.            
  70.             ExtenderProvidedPropertyAttribute attr = extender.Attributes[typeof(ExtenderProvidedPropertyAttribute)] as ExtenderProvidedPropertyAttribute;
  71.            
  72.             Debug.Assert(attr != null, "The original PropertyDescriptor does not have an ExtenderProvidedPropertyAttribute");
  73.            
  74.            
  75.             ReflectPropertyDescriptor reflectDesc = attr.ExtenderProperty as ReflectPropertyDescriptor;
  76.            
  77.             Debug.Assert(reflectDesc != null, "The original PropertyDescriptor has an invalid ExtenderProperty");
  78.            
  79.             this.extenderInfo = reflectDesc;
  80.             this.provider = attr.Provider;
  81.         }
  82.        
  83.         /// <devdoc>
  84.         /// Determines if the the component will allow its value to be reset.
  85.         /// </devdoc>
  86.         public override bool CanResetValue(object comp)
  87.         {
  88.             return extenderInfo.ExtenderCanResetValue(provider, comp);
  89.         }
  90.        
  91.         /// <devdoc>
  92.         /// Retrieves the type of the component this PropertyDescriptor is bound to.
  93.         /// </devdoc>
  94.         public override Type ComponentType {
  95.             get { return extenderInfo.ComponentType; }
  96.         }
  97.        
  98.         /// <devdoc>
  99.         /// Determines if the property can be written to.
  100.         /// </devdoc>
  101.         public override bool IsReadOnly {
  102.             get { return Attributes[typeof(ReadOnlyAttribute)].Equals(ReadOnlyAttribute.Yes); }
  103.         }
  104.        
  105.         /// <devdoc>
  106.         /// Retrieves the data type of the property.
  107.         /// </devdoc>
  108.         public override Type PropertyType {
  109.             get { return extenderInfo.ExtenderGetType(provider); }
  110.         }
  111.        
  112.         /// <devdoc>
  113.         /// Retrieves the display name of the property. This is the name that will
  114.         /// be displayed in a properties window. This will be the same as the property
  115.         /// name for most properties.
  116.         /// </devdoc>
  117.         public override string DisplayName {
  118.             get {
  119.                 string name = base.DisplayName;
  120.                
  121.                 DisplayNameAttribute displayNameAttr = Attributes[typeof(DisplayNameAttribute)] as DisplayNameAttribute;
  122.                 if (displayNameAttr == null || displayNameAttr.IsDefaultAttribute()) {
  123.                     ISite site = GetSite(provider);
  124.                     if (site != null) {
  125.                         string providerName = site.Name;
  126.                         if (providerName != null && providerName.Length > 0) {
  127.                             name = SR.GetString(SR.MetaExtenderName, name, providerName);
  128.                         }
  129.                     }
  130.                 }
  131.                 return name;
  132.             }
  133.         }
  134.        
  135.         /// <devdoc>
  136.         /// Retrieves the value of the property for the given component. This will
  137.         /// throw an exception if the component does not have this property.
  138.         /// </devdoc>
  139.         public override object GetValue(object comp)
  140.         {
  141.             return extenderInfo.ExtenderGetValue(provider, comp);
  142.         }
  143.        
  144.         /// <devdoc>
  145.         /// Resets the value of this property on comp to the default value.
  146.         /// </devdoc>
  147.         public override void ResetValue(object comp)
  148.         {
  149.             extenderInfo.ExtenderResetValue(provider, comp, this);
  150.         }
  151.        
  152.         /// <devdoc>
  153.         /// Sets the value of this property on the given component.
  154.         /// </devdoc>
  155.         public override void SetValue(object component, object value)
  156.         {
  157.             extenderInfo.ExtenderSetValue(provider, component, value, this);
  158.         }
  159.        
  160.         /// <devdoc>
  161.         /// Determines if this property should be persisted. A property is
  162.         /// to be persisted if it is marked as persistable through a
  163.         /// PersistableAttribute, and if the property contains something other
  164.         /// than the default value. Note, however, that this method will
  165.         /// return true for design time properties as well, so callers
  166.         /// should also check to see if a property is design time only before
  167.         /// persisting to runtime storage.
  168.         /// </devdoc>
  169.         public override bool ShouldSerializeValue(object comp)
  170.         {
  171.             return extenderInfo.ExtenderShouldSerializeValue(provider, comp);
  172.         }
  173.        
  174.        
  175.        
  176.         /*
  177.           The following code has been removed to fix FXCOP violations.  The code
  178.           is left here incase it needs to be resurrected in the future.
  179.         /// <devdoc>
  180.         ///    Creates a new extended property info.  Callers can then treat this as
  181.         ///    a standard property.
  182.         /// </devdoc>
  183.         public ExtendedPropertyDescriptor(ReflectPropertyDescriptor extenderInfo, Type receiverType, IExtenderProvider provider) : this(extenderInfo, receiverType, provider, null) {
  184.         }
  185.         /// <devdoc>
  186.         ///    Retrieves the object that is providing this extending property.
  187.         /// </devdoc>
  188.         public IExtenderProvider Provider {
  189.             get {
  190.                 return provider;
  191.             }
  192.         }
  193.         */       
  194.        
  195.     }
  196. }

Developer Fusion