getting a "default" concrete class that implements an interface

Posted by Roger Joys on Stack Overflow See other posts from Stack Overflow or by Roger Joys
Published on 2012-11-03T16:16:41Z Indexed on 2012/11/03 17:01 UTC
Read the original article Hit count: 237

Filed under:
|
|
|

I am implementing a custom (and generic) Json.net serializer and hit a bump in the road that I could use some help on.

When the deserializer is mapping to a property that is an interface, how can I best determine what sort of object to construct to deserialize to to place into the interface property.

I have the following:

[JsonConverter(typeof(MyCustomSerializer<foo>))]
class foo 
{
    int Int1 { get; set; }
    IList<string> StringList {get; set; }
}

My serializer properly serializes this object, and but when it comes back in, and I try to map the json parts to to object, I have a JArray and an interface.

I am currently instantiating anything enumerable like List as

 theList = Activator.CreateInstance(property.PropertyType);

This works create to work with in the deserialization process, but when the property is IList, I get runtime complaints (obviously) about not being able to instantiate an interface.

So how would I know what type of concrete class to create in a case like this?

Thank you

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET