MetadataTypeAttribute 类
定义
指定要与数据模型类关联的元数据类。Specifies the metadata class to associate with a data model class.
public ref class MetadataTypeAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false)]
public sealed class MetadataTypeAttribute : Attribute
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
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 元数据类与实体分部类相关联。The following example shows how to use the MetadataTypeAttribute to associate a metadata class with an entity partial class. 该示例将 RequiredAttribute 属性应用到数据字段,以说明如何在关联的元数据类中提供其他信息。The example applies the RequiredAttribute attribute to a data field to show how to provide additional information in the associated metadata class.
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特性使你能够将类与数据模型分部类相关联。The MetadataTypeAttribute attribute enables you to associate a class with a data-model partial class. 在此关联的类中,你将提供不在数据模型中的其他元数据信息。In this associated class you provide additional metadata information that is not in the data model.
例如,在关联的类中,您可以将 RequiredAttribute 属性应用于数据字段。For example, in the associated class you can apply the RequiredAttribute attribute to a data field. 这会强制为字段提供一个值,即使数据库架构不需要此约束也是如此。This enforces that a value is provided for the field even if this constraint is not required by the database schema.
使用属性, MetadataTypeAttribute 如下所示:You use the MetadataTypeAttribute attribute as follows:
在应用程序中,创建一个文件,用于创建要修改的数据模型分部类。In your application, create a file in which you create the data-model partial class that you want to modify.
创建关联的元数据类。Create the associated metadata class.
将 MetadataTypeAttribute 特性应用到分部实体类,并指定关联的类。Apply the MetadataTypeAttribute attribute to the partial entity class, specifying the associated class.
应用此特性时,必须遵循以下使用限制:When you apply this attribute, you must adhere to the following usage constraints:
特性只能应用于类。The attribute can only be applied to a class.
派生类不能继承该属性。The attribute cannot be inherited by derived classes.
特性只能应用一次。The attribute can be applied only one time.
构造函数
| MetadataTypeAttribute(Type) |
初始化 MetadataTypeAttribute 类的新实例。Initializes a new instance of the MetadataTypeAttribute class. |
属性
| MetadataClassType |
获取与数据模型分部类关联的元数据类。Gets the metadata class that is associated with a data-model partial class. |
| TypeId |
在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute. (继承自 Attribute) |
方法
| Equals(Object) |
返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object. (继承自 Attribute) |
| GetHashCode() |
返回此实例的哈希代码。Returns the hash code for this instance. (继承自 Attribute) |
| GetType() |
获取当前实例的 Type。Gets the Type of the current instance. (继承自 Object) |
| IsDefaultAttribute() |
在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (继承自 Attribute) |
| Match(Object) |
当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (继承自 Attribute) |
| MemberwiseClone() |
创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (继承自 Object) |
| ToString() |
返回表示当前对象的字符串。Returns a string that represents the current object. (继承自 Object) |
显式接口实现
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers. (继承自 Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface. (继承自 Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1). (继承自 Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object. (继承自 Attribute) |