IOC-resolving of ctor dependencies when deserializing

Jul 12, 2012 at 4:24 PM

I'm serializing an object including the full type name (TypeNameHandling = TypeNameHandling.All). When deserializing this I would like to use my IOC to create the instance. Is there any way to do this?

CustomCreationConverter doesn't seem to help here because I don't see the type there that was include in the JSON when serializing ($type).

Tobias

Aug 5, 2012 at 3:40 PM

What about using a custom contract resolver? Inherit from DefaultContractResolver and override CreateObjectContract. Not sure if it'll work but you can try.

protected override JsonObjectContract CreateObjectContract(Type objectType)       

{

JsonObjectContract contract = base.CreateObjectContract(objectType);

contract.DefaultCreator = () => IOCContainer.GetInstance(objectType);

return contract;

}