using System;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
namespace DeskApp2_0.Core
{
public class Common
{
[StructLayout(LayoutKind.Sequential)]
[DataContract] /* This [DataContract] is highlighted as not found. */
public struct SystemData
{
[DataMember] /* This [DataMember] is highlighted as not found. */
public byte Version;
[DataMember] /* This one as well. */
public ushort Name;
}
}
}
In order to be able to use the [DataContract] and [DataMember] attributes you’re going to need to:
using System.Runtime.Serialization;
That using is critical. But apparently you’ve added it to your file according to the code snippet you provided.
Make sure you are targeting .Net Framework version 4.0+ and above.
Add the “System.Runtime.Serialization” reference to your project.
Follow these steps:
Add a new reference on your solution explorer:
Past this System.Runtime.Serialization into the search box:
Check the appropriate reference.
Press OK and you’re done.
Sometimes even if you follow the steps above, Visual Studio might fail to compile the [DataContract] and [DataMember] attributes. What you can do in that case is:
Rebuild the entire solution (clean and then build).
Reload the project.
Close and reopen the Solution.
Restart Visual Studio.
If you are still facing some issue, then probably better to start a new topic and reference this topic. With the specifics of your issue.
Well… Visual Studio, has been quite verbose and explained why it’s highlighting your [DataContract] and [DataMember] attributes.
Read the error again:
The type or namespace name ‘DataContract’ could not be found (are you missing a using directive or an assembly reference?)
As you can see Visual Studio, is assuming that there are two possible reasons for that issue:
missing a using directive
As @isoftech mentioned, you should add the using System.Runtime.Serialization; and it seems like you’ve done that already.
an assembly reference
As @isoftech explains again on point #3 on his post, You should be adding the System.Runtime.Serialization reference to your project references. follow his tips.