DataAnnotations:Visual Studio不允许覆盖RequiresValidationContext属性

时间:2012-06-28 10:18:57

标签: .net-4.0 data-annotations visual-studio-2012 .net-4.5

我试图通过继承ValidationAttribute来实现自定义验证属性,并发现奇怪的事情 我的自定义属性需要验证上下文,所以我查看了ValidationAttribute的R#反编译源,看到我需要在我的自定义属性类中覆盖它:

public virtual bool RequiresValidationContext
{
  get
  {
    return false;
  }
}

现在有趣的部分 - Visual Studio 2012不允许我这样做,告诉我没有这样的属性可以覆盖,虽然在运行项目时,在调试视图中我可以看到该属性。

有趣的是,我在参考路径中看到:

  

C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETFramework \ v4.0 \ System.ComponentModel.DataAnnotations.dll

但是在R#反编译文件中路径不同:

  

程序集位置:C:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ System.ComponentModel.DataAnnotations.dll

因此,编译器在运行时使用不同的dll。 所以,我试图将参考dll切换到一个R#反编译,但看起来VS2012正在用它的版本替换它,在项目文件中看起来像:

<Reference Include="System.ComponentModel.DataAnnotations" />

因此,项目中没有保存路径。 没有手动编辑项目,有没有解决方案?

顺便说一句,4.5 NET它运行正常,我的意思是它看到了RequiresValidationContext属性。

更新:正如他们所说,图片胜过千言万语:

enter image description here

1 个答案:

答案 0 :(得分:1)

解决方案很简单,属性RequiresValidationContext已添加到.NET 4.5中的ValidationAttribute。由于您使用C#4.0进行编码/使用/开发,我怀疑您的目标是.NET Framework 4.0,在该版本中该属性执行not exist

因此您无法覆盖该属性,因为它不存在。