I have a class in C# that I am trying to serialize using a DataContractSerializer. It looks something like this:
namespace Foo
{
[DataContract(Name = "Bar")]
class Bar
{
class A
{
public A(object a, object b, object c)
{
d = a;
e = b;
f = c;
}
public object d;
public object e;
public object f;
}
[DataMember]
private ArrayList lst = new ArrayList();
...
}
}
When I try to serialize this class, I get an error:
"System.Runtime.Serialization.SerializationException: Type 'Foo.Bar+A' with data contract name '...' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types."
What I want to do is ignore A. It is just the definition of a type. None of the DataMembers I am trying to serialize are of type A.
Edit: This is the serialization code.
public void Serialize()
{
var writer = new FileStream("testoutput.xml", FileMode.Create);
var serializer = new DataContractSerializer(typeof(Bar));
serializer.WriteObject(writer, this);
writer.Close();
}
The exception is thrown on serializer.WriteObject.
After poking around a little bit more, I was able to fix it like this:
[KnownType(typeof(A))]
[DataContractAttribute]
class A
{
public A(object a, object b, object c)
{
d = a;
e = b;
f = c;
}
[DataMember]
public object d;
[DataMember]
public object e;
[DataMember]
public object f;
}
I think that one of the arraylists was actually holding As. This class is massive, so I probably just missed it.