1

I am trying to return a JSON object from a Web API call. I would like to return this object in terms of the interface it implements, so:

response.Content.ReadAsAsync<IThingy>().Result

I'm getting this error:

Could not create an instance of type IThingy. Type is an interface or abstract class and cannot be instantiated.

Really this is just the same as the problem in this question, except that I want to deserialize an object that implements an interface, rather than deserializing a property whose type implements an interface.

How can I do this deserialization?

1 Answer 1

3

You need to deserialize to a concrete class because the behaviour of that class must be defined. You need a Thingy : IThingy at which point I would do:

IThingy result = response.Content.ReadAsAsync<Thingy>().Result;

Why must you do this? Interfaces define no behaviour, only signatures of methods and properties. Say you were able to deserialize directly to IThingy and it had a method DoMath(int x, int y) - what math would be done? There would be no defined behaviour. That's why you must choose a concrete class.

Sign up to request clarification or add additional context in comments.

6 Comments

Yes, the interfaces I am passing are indeed signatures - because I am passing data transfer objects (no methods). Passing the interfaces reduces coupling on their actual implementation.
Of course I do have concrete classes, but I want to pass their instances in terms of the interfaces.
@Gigi this is a common misunderstanding. Interfaces are a signature for concrete types. Decoupling is all about SOLID AKA coupling to interfaces vs concrete types. However, at some point SOMETHING must know what types to use. In an inversion-of-control scenario the topmost class/startup will know and resolve dependencies. This deserialization is ANOTHER entry point for your application which must also know about your concrete types. This is normal. You cannot avoid deserializing to a concrete type unless you deserialize to a dynamic type, which I would surely not recommend.
If you're very concerned, just create an interface like IThingyDeserializer and have a concrete Deserializer<T> where T : Thingy which will deserialize to the concrete type Thingy... Then use that directly to keep concretes out. Note that this would be quite a lot of overkill, however.
Thanks. Just one question... then why is it okay to deserialize interface properties (as per link in question)?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.