ASP.NET MVC V2 - 巴迪班 [英] ASP.NET MVC V2 - Buddy Classes

查看:154
本文介绍了ASP.NET MVC V2 - 巴迪班的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有在ASP.NET MVC 2 preVIEW 1哥们类的例子吗?我似乎无法找到在MSDN上关于它的任何东西,但根据<一个href=\"http://weblogs.asp.net/scottgu/archive/2009/07/31/asp-net-mvc-v2-$p$pview-1-released.aspx\">ScottGu他们已经在最近的版本中添加的功能吧。

Does anyone have an example of buddy classes in ASP.NET MVC 2 Preview 1? I can't seem to find anything on the MSDN about it but according to ScottGu they've added functionality for it in the most recent release.

推荐答案

我相信你要找的是什么MetadataTypeAttribute。这是不是一件具体到MVC,但在3.5推出的DataAnnotations命名空间的一部分。它可以让你装点外部类本身的分部类的成员。

I believe what you're looking for is MetadataTypeAttribute. This isn't something that's specific to MVC but it part of the DataAnnotations namespace introduced in 3.5. It allows you to decorate members of a partial class external to the class itself.

举例来说,如果你有名为Customer生成的部分类的类型,想要的属性添加到它,你可以在同一个命名空间创建一个新的部分,并与MetadataType标签吧。然后用匹配的属性创建元数据类和装饰他们。

For instance, if you had a generated partial class type named Customer and wanted to add attributes to it, you could create a new partial in the same namespace and tag it with the MetadataType. Then create the Metadata class with matching attributes and decorate them.

/* Generated class */
public partial class Customer
{ 
  public string Name { get; set; } 
}


/* MetadataType decorated class */
[MetadataType(CustomerMetadata)]
public partial class Customer
{ /* ... */ }


/* Metadata type */
public class CustomerMetadata
{
  [Required(ErrorMessage = "Name is required")]
  public string Name { get; set; }
}

这篇关于ASP.NET MVC V2 - 巴迪班的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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