[DataContract] 的命名空间
我找不到用于 [DataContract]
和 [DataMember]
元素的命名空间。根据我的发现,似乎添加以下内容就足够了,但就我而言还不够。
using System;
using System.Runtime.Serialization;
这是我的代码片段:
using System;
using System.Runtime.Serialization;
namespace MyNamespace {
[DataContract]
public class Tuple<T1, T2> {
// A custom implementation of a Tuple
//...
//...
}
}
以及我得到的错误:
找不到类型或命名空间名称“DataContract”(是否缺少 using 指令或程序集引用?)
我是否没有使用正确的命名空间?
I can't find the namespace to use for [DataContract]
and [DataMember]
elements. According to what I've found, it seems that adding the following should be enough, but in my case it is not.
using System;
using System.Runtime.Serialization;
Here is a snippet of my code:
using System;
using System.Runtime.Serialization;
namespace MyNamespace {
[DataContract]
public class Tuple<T1, T2> {
// A custom implementation of a Tuple
//...
//...
}
}
And the error I get:
The type or namespace name 'DataContract' could not be found (are you missing a using directive or an assembly reference?)
Am I not using the right namespaces?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
DataContractAttribute
类位于System.Runtime.Serialization
命名空间中。您应该添加对 System.Runtime.Serialization.dll 的引用。但默认情况下不会引用该程序集。要将引用添加到您的项目中,您必须转到引用 ->在解决方案资源管理器中添加引用并手动添加程序集引用。
DataContractAttribute
Class is in theSystem.Runtime.Serialization
namespace.You should add a reference to
System.Runtime.Serialization.dll
. That assembly isn't referenced by default though. To add the reference to your project you have to go to References -> Add Reference in the Solution Explorer and add an assembly reference manually.http://msdn.microsoft.com/en-us/library/system。 runtime.serialization.datacontractattribute.aspx
DataContractAttribute 位于 System.Runtime.Serialization 命名空间中,您应该引用 System.Runtime.Serialization.dll。仅适用于 .Net >= 3
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx
DataContractAttribute is in System.Runtime.Serialization namespace and you should reference System.Runtime.Serialization.dll. It's only available in .Net >= 3
在.NET 4.0框架的Visual Studio中,
System.Runtime.Serialization
。。并且将不会显示错误。
In visual studio for .Net 4.0 framework,
System.Runtime.Serialization
.using System.Runtime.Serialization
. And the error will not be shown.[DataContract] 和 [DataMember] 属性位于 System.ServiceModel.dll 中的 System.ServiceModel 命名空间中。
System.ServiceModel 使用 System 和 System.Runtime.Serialization 命名空间来序列化数据成员。
[DataContract] and [DataMember] attribute are found in System.ServiceModel namespace which is in System.ServiceModel.dll .
System.ServiceModel uses the System and System.Runtime.Serialization namespaces to serialize the datamembers.
我通过在参考中添加 C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\System.Runtime.Serialization.dll 解决了这个问题
I solved this problem by adding C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\System.Runtime.Serialization.dll in the reference
首先,我将引用添加到模型中,然后在代码中使用它们。您应该添加两个参考文献:
然后,这个问题在我的程序中得到了解决。我希望这个答案可以帮助你。谢谢。
First, I add the references to my Model, then I use them in my code. There are two references you should add:
then, this problem was solved in my program. I hope this answer can help you. Thanks.