DesignOnlyAttribute 类  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定某个属性是否只能在设计时设置。
public ref class DesignOnlyAttribute sealed : Attribute[System.AttributeUsage(System.AttributeTargets.All)]
public sealed class DesignOnlyAttribute : Attributepublic sealed class DesignOnlyAttribute : Attribute[<System.AttributeUsage(System.AttributeTargets.All)>]
type DesignOnlyAttribute = class
    inherit Attributetype DesignOnlyAttribute = class
    inherit AttributePublic NotInheritable Class DesignOnlyAttribute
Inherits Attribute- 继承
- 属性
示例
以下示例创建一个 GetLanguage 属性。 属性标记为 DesignOnlyAttribute。
public:
   [DesignOnly(true)]
   property CultureInfo^ GetLanguage 
   {
      CultureInfo^ get()
      {
         // Insert code here.
         return myCultureInfo;
      }
      void set( CultureInfo^ value )
      {
         // Insert code here.
      }
   }
[DesignOnly(true)]
 public CultureInfo GetLanguage {
    get {
       // Insert code here.
       return myCultureInfo;
    }
    set {
       // Insert code here.
    }
 }
<DesignOnly(True)> _
Public Property GetLanguage() As CultureInfo
    Get
        ' Insert code here.
        Return myCultureInfo
    End Get
    Set
        ' Insert code here.
    End Set
End Property
下一个示例获取 的值 DesignOnlyAttribute。 首先,代码获取具有 PropertyDescriptorCollection 对象的所有属性的 。 接下来,它会索引到 中 PropertyDescriptorCollection 以查找 GetLanguage 属性。 然后,它将返回此属性的属性,并将其保存在 attributes 变量中。
然后,该示例通过从 AttributeCollection检索 DesignOnlyAttribute 并将其写入控制台屏幕来打印属性是否仅为设计。
// Gets the attributes for the property.
AttributeCollection^ attributes = TypeDescriptor::GetProperties( this )[ "GetLanguage" ]->Attributes;
/* Prints whether the property is marked as DesignOnly 
by retrieving the DesignOnlyAttribute 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)["GetLanguage"].Attributes;
 /* Prints whether the property is marked as DesignOnly 
  * by retrieving the DesignOnlyAttribute 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.
Dim attributes As AttributeCollection = _
    TypeDescriptor.GetProperties(Me)("GetLanguage").Attributes
' Prints whether the property is marked as DesignOnly 
' by retrieving the DesignOnlyAttribute from the AttributeCollection.
Dim myAttribute As DesignOnlyAttribute = _
    CType(attributes(GetType(DesignOnlyAttribute)), DesignOnlyAttribute)
Console.WriteLine(("This property is design only :" & _
    myAttribute.IsDesignOnly.ToString()))
注解
标记为 设置为 DesignOnlyAttributetrue 的成员只能在设计时设置。 通常,这些属性仅在设计时存在,在运行时不对应于对象上的实际属性。
没有属性或标记为 DesignOnlyAttribute 集 false 的成员可以在运行时进行设置。 默认值为 false。
设置为 DesignOnlyAttributetrue 的属性将其值序列化为 .resx 文件,而不是 InitializeComponent 方法。
有关更多信息,请参阅特性。
构造函数
| DesignOnlyAttribute(Boolean) | 初始化 DesignOnlyAttribute 类的新实例。 | 
字段
| Default | 指定 DesignOnlyAttribute 的默认值,即 No。 此  | 
| No | 指定某个属性可在设计时或运行时设置。 此  | 
| Yes | 指定某个属性只能在设计时设置。 此  | 
属性
| IsDesignOnly | 获取一个值,该值指示属性是否只能在设计时设置。 | 
| TypeId | 在派生类中实现时,获取此 Attribute 的唯一标识符。(继承自 Attribute) | 
方法
| Equals(Object) | 返回给定对象的值是否等于当前的 DesignOnlyAttribute。 | 
| GetHashCode() | 返回此实例的哈希代码。 | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| IsDefaultAttribute() | 确定此特性是否为默认特性。 | 
| IsDefaultAttribute() | 在派生类中重写时,指示此实例的值是否是派生类的默认值。(继承自 Attribute) | 
| Match(Object) | 当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。(继承自 Attribute) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) | 
显式接口实现
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) | 将一组名称映射为对应的一组调度标识符。(继承自 Attribute) | 
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) | 检索对象的类型信息,然后可以使用该信息获取接口的类型信息。(继承自 Attribute) | 
| _Attribute.GetTypeInfoCount(UInt32) | 检索对象提供的类型信息接口的数量(0 或 1)。(继承自 Attribute) | 
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) | 提供对某一对象公开的属性和方法的访问。(继承自 Attribute) |