MetadataTypeAttribute 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
指定要與數據模型類別建立關聯的元數據類別。
public ref class MetadataTypeAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false, Inherited=false)]
public sealed class MetadataTypeAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false)]
public sealed class MetadataTypeAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false, Inherited=false)>]
type MetadataTypeAttribute = class
inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false)>]
type MetadataTypeAttribute = class
inherit Attribute
Public NotInheritable Class MetadataTypeAttribute
Inherits Attribute
- 繼承
- 屬性
範例
以下範例展示如何利用 將 MetadataTypeAttribute 元資料類別與實體部分類別關聯。 範例將屬性 RequiredAttribute 套用到資料欄位,展示如何在相關的元資料類別中提供額外資訊。
using System;
using System.Web.DynamicData;
using System.ComponentModel.DataAnnotations;
[MetadataType(typeof(CustomerMetaData))]
public partial class Customer
{
}
public class CustomerMetaData
{
// Apply RequiredAttribute
[Required(ErrorMessage = "Title is required.")]
public object Title;
}
Imports System.Web.DynamicData
Imports System.ComponentModel.DataAnnotations
<MetadataType(GetType(CustomerMetadata))> _
Partial Public Class Customer
End Class
Public Class CustomerMetadata
' Apply RequitedAttribute.
<Required(ErrorMessage:="Title is required.")> _
Public Title As Object
End Class
備註
這個 MetadataTypeAttribute 屬性讓你能將類別與資料模型部分類別關聯起來。 在這個相關類別中,你提供了資料模型中沒有的額外元資料資訊。
例如,在相關類別中,你可以將屬性套用 RequiredAttribute 到資料欄位。 這確保即使資料庫結構不需要這個限制,欄位仍必須提供一個值。
你用 MetadataTypeAttribute 這個屬性的方式如下:
在你的應用程式中,建立一個檔案,裡面建立你想修改的資料模型部分類別。
建立相關的元資料類別。
將屬性 MetadataTypeAttribute 套用到部分實體類別,指定所關聯的類別。
套用此屬性時,必須遵守以下使用限制:
該屬性只能套用到類別上。
該屬性無法被導出類別繼承。
該屬性只能套用一次。
建構函式
| 名稱 | Description |
|---|---|
| MetadataTypeAttribute(Type) |
初始化 MetadataTypeAttribute 類別的新執行個體。 |
屬性
| 名稱 | Description |
|---|---|
| MetadataClassType |
取得與資料模型部分類別相關的元資料類別。 |
| TypeId |
在衍生類別中實作時,取得這個 Attribute的唯一標識碼。 (繼承來源 Attribute) |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
傳回值,這個值表示這個實例是否等於指定的物件。 (繼承來源 Attribute) |
| GetHashCode() |
傳回這個實例的哈希碼。 (繼承來源 Attribute) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| IsDefaultAttribute() |
在衍生類別中覆寫時,指出這個實例的值是否為衍生類別的預設值。 (繼承來源 Attribute) |
| 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) |