我怎么能在一个分部类的另一半定义的属性使用属性? [英] How can I use attributes on a property defined in the other half of a partial class?

查看:159
本文介绍了我怎么能在一个分部类的另一半定义的属性使用属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自动生成的类从导入包含像这样一个Web服务(略):

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

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

和我想的MVC格式属性添加到该成员。因此,在含相同部分类定义另一个文件,我愿做类似如下(这是非法的):

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

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

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

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

我怎么能装点这个成员?

How can I decorate this member?

推荐答案

您可以使用 MetadataType 属性是这样的:

You could use MetadataType attribute like this:

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

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

这篇关于我怎么能在一个分部类的另一半定义的属性使用属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆