DesignOnlyAttribute 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
指定屬性是否只能在設計時間設定。
public ref class DesignOnlyAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.All)]
public sealed class DesignOnlyAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.All)>]
type DesignOnlyAttribute = class
inherit Attribute
Public 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.
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值為 。 首先,程式碼會取得包含物件所有屬性的 a PropertyDescriptorCollection 。 接著它會索引到該PropertyDescriptorCollectionGetLanguage物業。 接著它會回傳這個屬性的屬性,並儲存在屬性變數中。
範例接著透過從 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()))
備註
標示 DesignOnlyAttribute 為 的 true 構件只能在設計時設定。 通常,這些屬性只存在於設計時,且不對應執行時物件上的實體屬性。
沒有屬性或標記為 DesignOnlyAttribute 的 false 成員可在執行時被設定。 預設值為 false。
將 DesignOnlyAttribute 為 的 true 屬性,其值會序列化到 .resx 檔案,而非方法本身 InitializeComponent 。
如需詳細資訊,請參閱屬性。
建構函式
| 名稱 | Description |
|---|---|
| DesignOnlyAttribute(Boolean) |
初始化 DesignOnlyAttribute 類別的新執行個體。 |
欄位
| 名稱 | Description |
|---|---|
| Default |
指定 的預設值 DesignOnlyAttribute,即 No。 此 |
| No |
指定屬性可在設計時或執行時設定。 此 |
| Yes |
規定屬性只能在設計時設定。 此 |
屬性
| 名稱 | Description |
|---|---|
| IsDesignOnly |
會得到一個值,表示某屬性是否只能在設計時設定。 |
| TypeId |
在衍生類別中實作時,取得這個 Attribute的唯一標識碼。 (繼承來源 Attribute) |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
回傳給定物件的值是否等於當前 DesignOnlyAttribute值。 |
| GetHashCode() |
傳回這個實例的哈希碼。 |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| IsDefaultAttribute() |
判斷此屬性是否為預設值。 |
| Match(Object) |
在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。 (繼承來源 Attribute) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |
明確介面實作
| 名稱 | Description |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。 (繼承來源 Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
擷取 物件的型別資訊,可用來取得介面的類型資訊。 (繼承來源 Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
擷取物件提供的類型資訊介面數目 (0 或 1)。 (繼承來源 Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供物件所公開屬性和方法的存取權。 (繼承來源 Attribute) |