Search code examples
c#.netusing.net-4.8

Using System.Runtime.Serialization but IgnoreDataMemberAttribute and DataMemberAttribute not found


I am trying to get this small JSONParser to work. Basicaly you just copy the code into your project. The only thing i changed is the namespace but i get errors in the following part:

 T member = members[i];
 if (member.IsDefined(typeof(IgnoreDataMemberAttribute), true)) //error for IgnoreDataMemberAttribute
     continue;

 string name = member.Name;
 if (member.IsDefined(typeof(DataMemberAttribute), true)) // error for DataMemberAttribute
 {
     DataMemberAttribute dataMemberAttribute = (DataMemberAttribute)Attribute.GetCustomAttribute(member, typeof(DataMemberAttribute), true); // error for DataMemberAttribute
     if (!string.IsNullOrEmpty(dataMemberAttribute.Name))
         name = dataMemberAttribute.Name;
 }

This results in:

error CS0246: Der Typ- oder Namespacename "DataMemberAttribute" wurde nicht gefunden
error CS0246: Der Typ- oder Namespacename "IgnoreDataMemberAttribute" wurde nicht gefunden

But i have using System.Runtime.Serialization; at the top of my script and at another part the import works fine.

object instance = FormatterServices.GetUninitializedObject(type); //no error here

What is going wrong here?


I tried moving the using-directives into the namespace to make sure there was nothing populating the namespace but it did not change anything.

Strangely Visual Studio suggests using System.Runtime.Serialization but when i click it Visual Studio is just showing it doing it but nothing happens.


Solution

  • As @Ian Kemp said in the comments i had to add a reference to the dll. Normaly i work with rider where this happens automaticaly.