如何在部分类的另一半中定义的属性上使用属性?

时间:2021-02-10 21:34:06

I have an autogenerated class from importing a web service containing something like this (abbreviated):

我有一个自动生成的类来导入包含这样的(缩写)的Web服务:

[System.Runtime.Serialization.DataMemberAttribute()]
public System.DateTime StartDate 
{
    get 
    {
        return this.StartDateField;
    }
    set { /* implementation prop changed */ }
}

And I want to add an MVC format attribute to this member. So in another file containing the same partial class definition, I would like to do something like the following (which is illegal):

我想为这个成员添加一个MVC格式属性。所以在另一个包含相同部分类定义的文件中,我想做类似以下的事情(这是非法的):

[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)] 
public DateTime StartDate;

A partial method is of no use here because partial methods must be private, have void return type, must be a method etc etc.

部分方法在这里没有用,因为部分方法必须是私有的,具有void返回类型,必须是方法等等。

How can I decorate this member?

我该如何装饰这个会员?

1 个解决方案

#1


10  

You could use MetadataType attribute like this:

您可以使用MetadataType属性,如下所示:

[MetadataType(typeof(MyClass_Validation))]     
public partial class MyClass
{} 

public class MyClass_Validation     
{     
   [DisplayFormat(...)] 
   public DateTime StartDate { get; set; } 
}

#1


10  

You could use MetadataType attribute like this:

您可以使用MetadataType属性,如下所示:

[MetadataType(typeof(MyClass_Validation))]     
public partial class MyClass
{} 

public class MyClass_Validation     
{     
   [DisplayFormat(...)] 
   public DateTime StartDate { get; set; } 
}