为什么我不能引用System.ComponentModel.DataAnnotations?

我想在我的WPF项目中使用DataAnnotations指定string的最大长度,具体如下:

using System.ComponentModel.DataAnnotations; 

但是,我得到的错误

名称空间“System.ComponentModel”中不存在types或名称空间名称“DataAnnotations”(是否缺less程序集引用?)

我已经看到了DataAnnotations存在于这个命名空间的其他例子。 我正在使用C#4。 有什么理由不能用这个吗? 我能做些什么来解决它?

您必须引用定义了此名称空间的程序集(它在Visual Studio模板中未默认引用)。 打开你的引用pipe理器,并添加一个对System.ComponentModel.DataAnnotations程序集的引用(Solution explorer – > Add reference – > Select .Net tab – >从列表中selectSystem.ComponentModel.DataAnnotations)

我也有同样的问题,我通过在我没有提到的参考项目之一的项目中添加引用来解决。 如果您的解决scheme中包含2-3个项目,请将该参考添加到其他项目中。

引用System.ComponentModel.DataAnnotations

在一个代码文件中使用System.ComponentModel.DataAnnotations; 在文件的顶部,如:

 using System.ComponentModel.DataAnnotations; 

通过右键单击解决scheme资源pipe理器中的项目,将.NET引用添加到项目中:

在这里输入图像说明

希望这可以帮助! 这个问题帮助了我。

使用框架版本4.5及以上为您的项目,然后解决问题。因为这个命名空间低于4.5及以上。

我发现我不能引用Silverlight 5中的System.ComponentModel.DataAnnotations和(1)中的下面的版本。 我发现Silverlight 5程序集不能使用.NET程序集,它给出了错误“您不能添加对System.ComponentModel.DataAnnotations的引用,因为它不是针对Silverlight运行时构build的…”我计​​划解决这个问题希望安装下面(2)处的Silverlight 5软件包。 如果失败,我会更新这个post。

[更新:失败。 我安装了所有与Silverlight 5有关的东西,而我没有Silverlight版本的.dll程序集System.ComponentModel.DataAnnotations。 太糟糕了。 更新二:我从Visual Studio 2008或2010的Silverlight开发人员工具包的以前安装中find了一个旧的.dll。我添加了这个文件,似乎“工作”,因为IntelliSense现在正在识别类成员上的属性,如[Display(Name = "My Property Name")]. 这个.dll是否适用于其他所有的东西,我不知道。]

(1)

 Microsoft Visual Studio Professional 2013 Version 12.0.21005.1 REL Microsoft .NET Framework Version 4.5.51641 

安装的版本:专业

(2) http://go.microsoft.com/fwlink/?LinkId=229318

 This error occurs when the reference to the "System.dll" got removed.Solution to the problem is very simple add the reference to "System.dll".The dll is normally available in the following location "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727" .Add the reference your problem will get solved . 

如果您尝试将vs2008的visual studio更新为vs2010。 而你的应用程序使用框架3.5(你不想升级它),也使用WCF RIA服务testing…我有坏消息…你必须升级到WCF RIA服务v1(testing版不适用于vs2010 )…由于这…你还必须安装Silverlight 4 +升级到框架4.0

看到这个: http : //blog.nappisite.com/2010/05/updating-visual-studio-2008net-35-ria.html

我从Silverlight 4升级到Silverlight 5,然后我遇到了这个问题。 尽pipe我在项目的“References”下有一个对“System.ComponentModel.DataAnnotations”的引用,但是它有一个黄色的yield符号,表示之前引用的程序集找不到。 事实certificate,“System.ComponentModel.DataAnnotations”引用的属性表示“Specific Version = True”,当我将其更改为“Specific Version = False”时,它解决了这个问题。 右键单击“参考”下的“System.ComponentModel.DataAnnotations”程序集,然后从上下文菜单中select“属性”。 检查“Specific Version = False”的属性值。

它必须引用旧的Silverlight 4程序集,而Silverlight 4程序集在升级到Silverlight 5后不再可用。

System.ComponentModel.DataAnnotations包含在它自己的程序集中,所以你需要确保你有它的参考。 简而言之:

1)。 右键单击Soloution并select添加。

2)。 从列表中select参考。

3)。 search“System.ComponentModel.DataAnnotation”并勾选其左侧的checkbox,然后按确定。

工作完成,不应该有任何引用错误。

我有同样的问题,我通过以下方式解决了这个问题。

右键点击页面,select属性。 在构build操作中select内容。

希望这个解决scheme可以帮助你。