MetadataTypeAttribute 类
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定要与数据模型类关联的元数据类。
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 将元数据类与实体分部类相关联。 该示例将 特性应用于 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 特性应用于分部实体类,并指定关联的类。
应用此属性时,必须遵守以下使用约束:
特性只能应用于类。
派生类不能继承 特性。
特性只能应用一次。
Metadata |
初始化 MetadataTypeAttribute 类的新实例。 |
Metadata |
获取与数据模型分部类关联的元数据类。 |
Type |
在派生类中实现时,获取此 Attribute 的唯一标识符。 (继承自 Attribute) |
Equals(Object) |
返回一个值,该值指示此实例是否与指定的对象相等。 (继承自 Attribute) |
Get |
返回此实例的哈希代码。 (继承自 Attribute) |
Get |
获取当前实例的 Type。 (继承自 Object) |
Is |
在派生类中重写时,指示此实例的值是否是派生类的默认值。 (继承自 Attribute) |
Match(Object) |
当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。 (继承自 Attribute) |
Memberwise |
创建当前 Object 的浅表副本。 (继承自 Object) |
To |
返回表示当前对象的字符串。 (继承自 Object) |
_Attribute. |
将一组名称映射为对应的一组调度标识符。 (继承自 Attribute) |
_Attribute. |
检索对象的类型信息,然后可以使用该信息获取接口的类型信息。 (继承自 Attribute) |
_Attribute. |
检索对象提供的类型信息接口的数量(0 或 1)。 (继承自 Attribute) |
_Attribute. |
提供对某一对象公开的属性和方法的访问。 (继承自 Attribute) |
产品 | 版本 |
---|---|
.NET | Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |