Namespace for [DataContract]

Otiel picture Otiel · Sep 13, 2011 · Viewed 206.4k times · Source

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?

Answer

CD.. picture CD.. · Sep 13, 2011

DataContractAttribute Class is in the System.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.