C# 为什么我不能引用 System.ComponentModel.DataAnnotations?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10174420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why can't I reference System.ComponentModel.DataAnnotations?
提问by DaveDev
I'm trying to use DataAnnotations in my WPF project to specify a maximum length of strings, with the following:
我试图在我的 WPF 项目中使用 DataAnnotations 来指定字符串的最大长度,如下所示:
using System.ComponentModel.DataAnnotations;
However, I get the error
但是,我收到错误
The type or namespace name 'DataAnnotations' does not exist in the namespace 'System.ComponentModel' (are you missing an assembly reference?)
命名空间“System.ComponentModel”中不存在类型或命名空间名称“DataAnnotations”(您是否缺少程序集引用?)
I've seen other examples where DataAnnotationsdoesexist in this namespace. I'm using C#4. Is there any reason why I can't use this? What can I do to fix it?
我已经看到其他例子在这个命名空间中DataAnnotations确实存在。我正在使用 C#4。有什么原因我不能使用它吗?我能做些什么来修复它?
采纳答案by Hari
You have to reference the assembly in which this namespace is defined (it is not referenced by default in the visual studio templates). Open your reference manager and add a reference to the System.ComponentModel.DataAnnotations assembly (Solution explorer -> Add reference -> Select .Net tab -> select System.ComponentModel.DataAnnotations from the list)
您必须引用定义此命名空间的程序集(在 Visual Studio 模板中默认不引用它)。打开您的引用管理器并添加对 System.ComponentModel.DataAnnotations 程序集的引用(解决方案资源管理器 -> 添加引用 -> 选择 .Net 选项卡 -> 从列表中选择 System.ComponentModel.DataAnnotations)
回答by Pankil Agrawal
I had same problem, I solved this problem by following way.
我有同样的问题,我通过以下方式解决了这个问题。
Right click on page, select Property. in build action select Content.
右键单击页面,选择属性。在构建操作中选择内容。
Hope that this solution may help you.
希望这个解决方案可以帮助你。
回答by Fernando Torres
If you tried to update visual studio from vs2008 to vs2010. And your app uses framework 3.5 (and you don't want to upgrade it), and also used WCF RIA Services BETA... I have bad news... you MUST upgrade to WCF RIA Services v1 (BETA does not work on vs2010)... and due to this... you also have to install Silverlight 4 + upgrade to framework 4.0
如果您尝试将 Visual Studio 从 vs2008 更新到 vs2010。并且您的应用程序使用框架 3.5(并且您不想升级它),并且还使用了 WCF RIA Services BETA...我有一个坏消息...您必须升级到 WCF RIA Services v1(BETA 不适用于 vs2010 )...因此...您还必须安装 Silverlight 4 + 升级到框架 4.0
See this: http://blog.nappisite.com/2010/05/updating-visual-studio-2008net-35-ria.html
看到这个:http: //blog.nappisite.com/2010/05/updating-visual-studio-2008net-35-ria.html
回答by user2090762
Use the FrameWork version 4.5 and above for your project then problem solved.Because this namespace is under 4.5 and above.
为您的项目使用 FrameWork 4.5 及更高版本,然后问题解决。因为此命名空间低于 4.5 及更高版本。
回答by Catto
To Reference System.ComponentModel.DataAnnotations
引用 System.ComponentModel.DataAnnotations
In a code file to have Using System.ComponentModel.DataAnnotations; at the top of the file such as:
在代码文件中使用 System.ComponentModel.DataAnnotations; 在文件的顶部,例如:
using System.ComponentModel.DataAnnotations;
Add a .NET reference to your project by right clicking the project in solution explorer:
通过右键单击解决方案资源管理器中的项目,向项目添加 .NET 引用:


Hope this helps! This question helped me.
希望这可以帮助!这个问题对我有帮助。
回答by PaulDecember
I found that I cannot reference System.ComponentModel.DataAnnotations from Silverlight 5 with the below version at (1). I found that Silverlight 5 assemblies cannot use .NET assemblies, it gives the error "You can't add a reference to System.ComponentModel.DataAnnotations as it was not built against the Silverlight runtime. ..." I plan to workaround this by hopefully installing the Silverlight 5 package found at (2) below. If this fails I will update this post.
我发现我无法从 Silverlight 5 中引用 System.ComponentModel.DataAnnotations 和以下版本 (1)。我发现 Silverlight 5 程序集不能使用 .NET 程序集,它给出了错误“你不能添加对 System.ComponentModel.DataAnnotations 的引用,因为它不是针对 Silverlight 运行时构建的。......”我计划解决这个问题希望安装在下面 (2) 中找到的 Silverlight 5 包。如果这失败了,我会更新这篇文章。
[UPDATE: it failed. I installed everything relating to Silverlight 5 and I don't have the Silverlight version of the .dll assembly System.ComponentModel.DataAnnotations . Too bad. UPDATE II: I found an old .dll having this name from a previous installation of Silverlight developer's kit for Visual Studio 2008 or 2010. I added this file and it seems to 'work', in that IntelliSense is now recognizing attributes on class members, such as [Display(Name = "My Property Name")].Whether or not this works for everything else in this .dll I don't know.]
[更新:失败了。我安装了与 Silverlight 5 相关的所有内容,但我没有 .dll 程序集 System.ComponentModel.DataAnnotations 的 Silverlight 版本。太糟糕了。更新 II:我从以前安装的用于 Visual Studio 2008 或 2010 的 Silverlight 开发人员工具包中找到了一个具有此名称的旧 .dll。我添加了这个文件,它似乎“有效”,因为 IntelliSense 现在正在识别类成员的属性,例如这 [Display(Name = "My Property Name")].是否适用于此 .dll 中的其他所有内容,我不知道。]
(1)
(1)
Microsoft Visual Studio Professional 2013
Version 12.0.21005.1 REL
Microsoft .NET Framework
Version 4.5.51641
Installed Version: Professional
安装版本:专业版
回答by Ashish Sahu
I also had the same problem and I resolved by adding the reference in one of my projects which didn't had the mentioned reference. If you have 2-3 projects in your solution, then check by adding this reference to the other projects.
我也遇到了同样的问题,我通过在我的一个没有提到的参考的项目中添加参考来解决。如果您的解决方案中有 2-3 个项目,请通过将此引用添加到其他项目来进行检查。
回答by Ankit Agrawal
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 .
回答by cesiumdeth
I upgraded from Silverlight 4 to Silverlight 5 and then I was having this issue. Although I had a reference to "System.ComponentModel.DataAnnotations" under "References" in my project, it had a yellow yield sign by it that indicated the previously referenced assembly could not be found. It turned out that the properties of the "System.ComponentModel.DataAnnotations" reference indicated "Specific Version = True", when I changed this to "Specific Version = False" it fixed the issue. Right click on the "System.ComponentModel.DataAnnotations" assembly under "References" and select "Properties" from the context menu. Check that the property value for "Specific Version = False".
我从 Silverlight 4 升级到 Silverlight 5,然后我遇到了这个问题。尽管我在我的项目中的“引用”下有对“System.ComponentModel.DataAnnotations”的引用,但它有一个黄色的产量标志,表明无法找到以前引用的程序集。事实证明,“System.ComponentModel.DataAnnotations”引用的属性指示“特定版本 = True”,当我将其更改为“特定版本 = False”时,它解决了该问题。右键单击“References”下的“System.ComponentModel.DataAnnotations”程序集,然后从上下文菜单中选择“Properties”。检查“特定版本 = False”的属性值。
It must have been referencing the old Silverlight 4 assembly which was no longer available after the upgrade to Silverlight 5.
它一定是引用了升级到 Silverlight 5 后不再可用的旧 Silverlight 4 程序集。
回答by whatdoyouNeedFromMe
System.ComponentModel.DataAnnotations is contained in its own assembly so you need to make sure you have it refernced. Just simply:
System.ComponentModel.DataAnnotations 包含在其自己的程序集中,因此您需要确保引用了它。简单地说:
1). Right click on Soloution and choose add.
1)。右键单击解决方案并选择添加。
2). Choose reference from the list.
2)。从列表中选择参考。
3). Search " System.ComponentModel.DataAnnotation " and tick the check box on its left hand side and press ok.
3)。搜索“ System.ComponentModel.DataAnnotation ”并勾选其左侧的复选框,然后按确定。
Job done, shouldnt have any refernce errors.
工作完成,不应该有任何参考错误。

