与code副属性在.NET中生成的属性 [英] Associate attribute with code generated property in .net

查看:141
本文介绍了与code副属性在.NET中生成的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在.NET中的公共属性设置的属性,但是我没有获得明确的财产本身,因为这已经在另一个文件中生成code。

I wish to set an attribute on a public property in .NET, however I do not have access to the explicit property itself, as this has been code generated in another file.

我有这样的领域:

public virtual string Name { get; set; }

我想设置此:

I wish to set this:

[ValidateNonEmpty("Name is required", ExecutionOrder = 1)]
public virtual string Name { get; set; }

我的类被标记为局部的,但你不能有偏性。我想我是上东西是动态数据和的DataAnnotations的一项新功能的MetadataType类,但可惜我觉得它只能与动态数据使用的,这是真的吗?

My class is marked as partial, but you cannot have partial properties. I thought I was on to something with the MetadataType class which is a new feature of Dynamic Data and DataAnnotations, but alas I feel it can only be used with Dynamic Data, is this true?

引文: <一href="http://blogs.oosterkamp.nl/blogs/jowen/archive/2008/10/16/metadatatype-attribute.aspx">http://blogs.oosterkamp.nl/blogs/jowen/archive/2008/10/16/metadatatype-attribute.aspx <一href="http://blogs.msdn.com/davidebb/archive/2008/06/16/dynamic-data-and-the-associated-metadata-class.aspx">http://blogs.msdn.com/davidebb/archive/2008/06/16/dynamic-data-and-the-associated-metadata-class.aspx

有什么办法,我可以设置该属性(甚至通过web.config中!)而不触及code生成的类?

Is there any way I can set this attributes (even through web.config!) without touching the code generated class?

在此先感谢, 格雷厄姆

Thanks in advance, Graham

推荐答案

这是一个已知的滋扰;你根本不能添加元数据生成的成员。

This is a known nuisance; you simply can't add metadata to the generated members.

有6个选项这里(为了增加工作的):

There are 6 options here (in order of increasing effort):

  • 如果你自己的属性,使其能够对类中声明它,例如: [ValidateNonEmpty(名称,名称是必需的,ExecutionOrder = 1)] - 再添加多个属性,以局部类定义
  • 使用虚拟/接口/等方法来查询这个,而不是通过属性
  • sublass生成的类型;覆盖或重新申报的成员,加入了元数据(真的很乱)
  • 使用自定义的 TypeDescriptionProvider 来提供动态的元数据(很多很多的工作) - 假设消费方面 TypeDescriptor ;最具约束力相关的消费者做的,但例如,防爆pression (许多LINQ提供程序使用)不
  • 修改code-发生器/写自己的
  • 尝试延长像PostSharp做的工作(我还没有找到一个办法做到这一点,但我爱听,如果你找到一个方法!)
  • if you own the attribute, make it possible to declare it against the class, for example: [ValidateNonEmpty("Name", "Name is required", ExecutionOrder = 1)] - then add multiple attributes to the partial class definition
  • use a virtual / interface / etc method to query this, rather than via attributes
  • sublass the generated type; override or re-declare the member, adding the metadata (really messy)
  • use a custom TypeDescriptionProvider to provide dynamic metadata (lots and lots of work) - assuming that the consumer respects TypeDescriptor; most binding-related consumers do, but for example, Expression (used by many LINQ providers) doesn't
  • change the code-generator / write your own
  • try to extend something like PostSharp to do the work (I haven't found a way to do this, but I've love to hear if you find a way!)

我通常与第一个选项成功,除非它是一个系统定义的属性( [显示名称] 等)。如果 [ValidateNonEmpty] 由动态数据定义,那么你可能无法做到这一点。

I usually have success with the first option, unless it is a system-defined attribute ([DisplayName], etc). If [ValidateNonEmpty] is defined by dynamic data, then you might not be able to do this.

这篇关于与code副属性在.NET中生成的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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