DesignOnlyAttribute 类

指定某个属性 (Property) 是否只能在设计时设置。

**命名空间:**System.ComponentModel
**程序集:**System(在 system.dll 中)

语法

声明
<AttributeUsageAttribute(AttributeTargets.All)> _
Public NotInheritable Class DesignOnlyAttribute
    Inherits Attribute
用法
Dim instance As DesignOnlyAttribute
[AttributeUsageAttribute(AttributeTargets.All)] 
public sealed class DesignOnlyAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::All)] 
public ref class DesignOnlyAttribute sealed : public Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.All) */ 
public final class DesignOnlyAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.All) 
public final class DesignOnlyAttribute extends Attribute

备注

通过将 DesignOnlyAttribute 设置为 true 进行标记的成员只能在设计时进行设置。通常,这些属性 (Property) 只能在设计时存在,并且不对应于运行时对象上的某个实际属性 (Property)。

没有属性 (Attribute) 或通过将 DesignOnlyAttribute 设置为 false 进行标记的成员可以在运行时进行设置。默认为 false

DesignOnlyAttribute 设置为 true 的属性的值被序列化为 .resx 文件而不是 InitializeComponent 方法。

有关更多信息,请参见 属性 (Attribute) 概述利用属性扩展元数据

示例

下面的示例创建 GetLanguage 属性 (Property)。该属性 (Property) 标记为 DesignOnlyAttribute

<DesignOnly(True)> _
Public Property GetLanguage() As CultureInfo
    Get
        ' Insert code here.
        Return myCultureInfo
    End Get
    Set
        ' Insert code here.
    End Set
End Property
[DesignOnly(true)]
 public CultureInfo GetLanguage {
    get {
       // Insert code here.
       return myCultureInfo;
    }
    set {
       // Insert code here.
    }
 }
public:
   [DesignOnly(true)]
   property CultureInfo^ GetLanguage 
   {
      CultureInfo^ get()
      {
         // Insert code here.
         return myCultureInfo;
      }
      void set( CultureInfo^ value )
      {
         // Insert code here.
      }
   }
/** @attribute DesignOnly(true)
 */
/** @property 
 */
public CultureInfo get_GetLanguage()
{
    // Insert code here.
    return myCultureInfo;
} //get_GetLanguage

/** @property 
 */
public void set_GetLanguage(CultureInfo value)
{
    // Insert code here.
} //set_GetLanguage

下一示例获取 DesignOnlyAttribute 的值。首先,代码获取具有该对象的所有属性 (Property) 的 PropertyDescriptorCollection。接下来,对 PropertyDescriptorCollection 进行索引,以查找 GetLanguage 属性 (Property)。然后它返回该属性 (Property) 的属性 (Attribute),并将它们保存到属性 (Attribute) 变量中。

然后该示例通过以下方法输出该属性 (Property) 是否只能在设计时设置:从 AttributeCollection 检索 DesignOnlyAttribute,然后将它写到控制台屏幕上。

' Gets the attributes for the property.
Dim attributes As AttributeCollection = _
    TypeDescriptor.GetProperties(Me)("GetLanguage").Attributes

' Prints the description by retrieving the DescriptionAttribute
' from the AttributeCollection. 
Dim myAttribute As DesignOnlyAttribute = _
    CType(attributes(GetType(DesignOnlyAttribute)), DesignOnlyAttribute)
Console.WriteLine(("This property is design only :" & _
    myAttribute.IsDesignOnly.ToString()))
// Gets the attributes for the property.
 AttributeCollection attributes = 
    TypeDescriptor.GetProperties(this)["GetLanguage"].Attributes;
 
 /* Prints the description by retrieving the DescriptionAttribute 
  * from the AttributeCollection. */
 DesignOnlyAttribute myAttribute = 
    (DesignOnlyAttribute)attributes[typeof(DesignOnlyAttribute)];
 Console.WriteLine("This property is design only :" +
    myAttribute.IsDesignOnly.ToString());
// Gets the attributes for the property.
AttributeCollection^ attributes = TypeDescriptor::GetProperties( this )[ "GetLanguage" ]->Attributes;

/* Prints the description by retrieving the DescriptionAttribute 
       * from the AttributeCollection. */
DesignOnlyAttribute^ myAttribute = dynamic_cast<DesignOnlyAttribute^>(attributes[ DesignOnlyAttribute::typeid ]);
Console::WriteLine( "This property is design only :{0}", myAttribute->IsDesignOnly );
// Gets the attributes for the property.
AttributeCollection attributes = TypeDescriptor.GetProperties(this).
    get_Item("GetLanguage").get_Attributes();

/* Prints the description by retrieving the DescriptionAttribute 
   from the AttributeCollection. 
 */
DesignOnlyAttribute myAttribute = (DesignOnlyAttribute)(attributes.
    get_Item(DesignOnlyAttribute.class.ToType()));

Console.WriteLine("This property is design only :" 
    + System.Convert.ToString(myAttribute.get_IsDesignOnly()));

继承层次结构

System.Object
   System.Attribute
    System.ComponentModel.DesignOnlyAttribute

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

DesignOnlyAttribute 成员
System.ComponentModel 命名空间
Attribute
PropertyDescriptor