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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="ToolboxItemAttribute.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. [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2113:SecureLateBindingMethods", Scope = "member", Target = "System.ComponentModel.ToolboxItemAttribute.get_ToolboxItemType():System.Type")]
  16. namespace System.ComponentModel
  17. {
  18.    
  19.     using System;
  20.     using System.Diagnostics;
  21.     using System.Globalization;
  22.     using System.Security.Permissions;
  23.    
  24.     /// <devdoc>
  25.     /// <para>
  26.     /// Specifies attributes for a toolbox item.
  27.     /// </para>
  28.     /// </devdoc>
  29.     [AttributeUsage(AttributeTargets.All)]
  30.     public class ToolboxItemAttribute : Attribute
  31.     {
  32.        
  33.         private Type toolboxItemType;
  34.         private string toolboxItemTypeName;
  35.        
  36.         /// <devdoc>
  37.         /// <para>
  38.         /// Initializes a new instance of ToolboxItemAttribute and sets the type to
  39.         /// IComponent.
  40.         /// </para>
  41.         /// </devdoc>
  42.         public static readonly ToolboxItemAttribute Default = new ToolboxItemAttribute("System.Drawing.Design.ToolboxItem, " + AssemblyRef.SystemDrawing);
  43.        
  44.         /// <devdoc>
  45.         /// <para>
  46.         /// Initializes a new instance of ToolboxItemAttribute and sets the type to
  47.         /// <see langword='null'/>.
  48.         /// </para>
  49.         /// </devdoc>
  50.         public static readonly ToolboxItemAttribute None = new ToolboxItemAttribute(false);
  51.        
  52.         /// <devdoc>
  53.         /// <para>
  54.         /// Gets whether the attribute is the default attribute.
  55.         /// </para>
  56.         /// </devdoc>
  57.         public override bool IsDefaultAttribute()
  58.         {
  59.             return this.Equals(Default);
  60.         }
  61.        
  62.         /// <devdoc>
  63.         /// <para>
  64.         /// Initializes a new instance of ToolboxItemAttribute and
  65.         /// specifies if default values should be used.
  66.         /// </para>
  67.         /// </devdoc>
  68.         public ToolboxItemAttribute(bool defaultType)
  69.         {
  70.             if (defaultType) {
  71.                 toolboxItemTypeName = "System.Drawing.Design.ToolboxItem, " + AssemblyRef.SystemDrawing;
  72.             }
  73.         }
  74.        
  75.         /// <devdoc>
  76.         /// <para>
  77.         /// Initializes a new instance of ToolboxItemAttribute and
  78.         /// specifies the name of the type.
  79.         /// </para>
  80.         /// </devdoc>
  81.         public ToolboxItemAttribute(string toolboxItemTypeName)
  82.         {
  83.             string temp = toolboxItemTypeName.ToUpper(CultureInfo.InvariantCulture);
  84.             Debug.Assert(temp.IndexOf(".DLL") == -1, "Came across: " + toolboxItemTypeName + " . Please remove the .dll extension");
  85.             this.toolboxItemTypeName = toolboxItemTypeName;
  86.         }
  87.        
  88.         /// <devdoc>
  89.         /// <para>
  90.         /// Initializes a new instance of ToolboxItemAttribute and
  91.         /// specifies the type of the toolbox item.
  92.         /// </para>
  93.         /// </devdoc>
  94.         public ToolboxItemAttribute(Type toolboxItemType)
  95.         {
  96.             this.toolboxItemType = toolboxItemType;
  97.             this.toolboxItemTypeName = toolboxItemType.AssemblyQualifiedName;
  98.         }
  99.        
  100.         /// <devdoc>
  101.         /// <para>
  102.         /// Gets the toolbox item's type.
  103.         /// </para>
  104.         /// </devdoc>
  105.         public Type ToolboxItemType {
  106.             get {
  107.                 if (toolboxItemType == null) {
  108.                     if (toolboxItemTypeName != null) {
  109.                         try {
  110.                             toolboxItemType = Type.GetType(toolboxItemTypeName, true);
  111.                         }
  112.                         catch (Exception ex) {
  113.                             throw new ArgumentException(SR.GetString(SR.ToolboxItemAttributeFailedGetType, toolboxItemTypeName), ex);
  114.                         }
  115.                     }
  116.                 }
  117.                 return toolboxItemType;
  118.             }
  119.         }
  120.        
  121.         public string ToolboxItemTypeName {
  122.             get {
  123.                 if (toolboxItemTypeName == null) {
  124.                     return String.Empty;
  125.                 }
  126.                 return toolboxItemTypeName;
  127.             }
  128.         }
  129.        
  130.         public override bool Equals(object obj)
  131.         {
  132.             if (obj == this) {
  133.                 return true;
  134.             }
  135.            
  136.             ToolboxItemAttribute other = obj as ToolboxItemAttribute;
  137.             return (other != null) && (other.ToolboxItemTypeName == ToolboxItemTypeName);
  138.         }
  139.        
  140.         public override int GetHashCode()
  141.         {
  142.             if (toolboxItemTypeName != null) {
  143.                 return toolboxItemTypeName.GetHashCode();
  144.             }
  145.             return base.GetHashCode();
  146.         }
  147.     }
  148. }

Developer Fusion