core icon indicating copy to clipboard operation
core copied to clipboard

Exception when serialize class contains Dictionary and DateTimeOffset via DataContractSerializer

Open jiemon opened this issue 9 years ago • 5 comments

I try serialize via DataContractSerializer simple class by example:

public class SimpleExampleClass2
		{
			private Dictionary<string, string> _attributes1;
			public Dictionary<string, string> Attributes1 => _attributes1;
			public DateTimeOffset TestDate { get; set; }
		}

Serialization code:

var test = new SimpleExampleClass2() { TestDate = DateTimeOffset.Now };
			var ser = new DataContractSerializer(typeof(SimpleExampleClass2));
			MemoryStream memoryStream = new MemoryStream();
			ser.WriteObject(memoryStream, test);
			memoryStream.Seek(0, SeekOrigin.Begin);
			return File(memoryStream, "application/xml", $"test.xml");

But on line "ser.WriteObject(memoryStream, test)" I get exception:

InvalidDataContractException: Type 'System.Runtime.Serialization.DateTimeOffsetAdapter' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

Is this bug in DataContractSerializer?

jiemon avatar Aug 04 '17 15:08 jiemon

@zhenlan @ViktorHofer @danmosemsft

Petermarcu avatar Aug 04 '17 15:08 Petermarcu

It's not related to binary serialization with BinaryFormatter.

ViktorHofer avatar Aug 04 '17 15:08 ViktorHofer

@zhenlan Any thoughts?

Petermarcu avatar Aug 04 '17 15:08 Petermarcu

+@shmao

zhenlan avatar Aug 04 '17 16:08 zhenlan

Issue moved to dotnet/corefx #22965 via ZenHub

shmao avatar Aug 04 '17 17:08 shmao