I have a generic class, PropertyCollection<TKey>. Because it contains a collection with values of object type, I have to use a surrogate. The trick is, I want to do this once for all possible types passed in TKey.
I have no problem doing this for a single type. If I want to use the surrogate when TKey is a MyEnum, I use the code block below and it works fine.
RuntimeTypeModel.Default
.Add(typeof(PropertyCollection<MyEnum>), false)
.SetSurrogate(typeof(PropertyCollectionSurrogate<MyEnum>));
My challenge is getting it to work with all values of TKey. I tried adding the type in a method decorated with the [OnSerializing] attribute, but this doesn't work because a serializer is already created.
I realize that each TKey is essentially a different method, so maybe I'm stuck.
Is there a way to register the surrogate using an attribute, that might follow the class around? Or can I effectively "roll my own" serialization using the [OnSerializing] and [OnDeserializing] attributes?
Anything would be really appreciated here!