I would like to set up a surrogate for Nullable. I looked at the various examples, and I think what I am doing is correct:
[ProtoContract]
public class DateTimeOffsetProxy
{
[ProtoMember(1)] public DateTime? UtcTime;
public static implicit operator DateTimeOffsetProxy(DateTimeOffset? value)
{
return new DateTimeOffsetProxy() {UtcTime = value?.UtcDateTime};
}
public static implicit operator DateTime?(DateTimeOffsetProxy value)
{
return value.UtcTime;
}
}
I am then doing this is a static constructor:
Model = RuntimeTypeModel.Default;
Model.AllowParseableTypes = true;
Model.Add(typeof(DateTimeOffset?),false)
.SetSurrogate(typeof(DateTimeOffsetProxy));
At the call to Add, I am getting this exception:
System.ArgumentException : Data of this type has inbuilt behaviour, and cannot be added to a model in this way: System.Nullable`1[[System.DateTimeOffset, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
I'm not sure why it is saying there is inbuilt behavior since the reason I was trying this was to resolve this exception serializing a class with a DateTimeOffset:
----> System.InvalidOperationException : No serializer defined for type: System.DateTimeOffset
We use DateTimeOffset and Nullable many places in our code, so I would like to come up with some way to handle this generically.
Can someone point out my error?