MetadataTypeAttribute 類別

定義

指定與資料模型類別產生關聯的中繼資料類別。

public ref class MetadataTypeAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false)]
public sealed class MetadataTypeAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false, Inherited=false)]
public sealed class MetadataTypeAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false)>]
type MetadataTypeAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false, Inherited=false)>]
type MetadataTypeAttribute = class
    inherit Attribute
Public NotInheritable Class MetadataTypeAttribute
Inherits Attribute
繼承
MetadataTypeAttribute
屬性

範例

下列範例示範如何使用 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 屬性套用至部分實體類別,並指定相關聯的類別。

當您套用此屬性時,必須遵守下列使用條件約束:

  • 屬性只能套用至類別。

  • 衍生類別無法繼承屬性。

  • 屬性只能套用一次。

建構函式

MetadataTypeAttribute(Type)

初始化 MetadataTypeAttribute 類別的新執行個體。

屬性

MetadataClassType

取得與資料模型部分類別相關聯的中繼資料類別。

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_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)

適用於