Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8771821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:53:12+00:00 2026-06-13T17:53:12+00:00

I am implementing a custom (and generic) Json.net serializer and hit a bump in

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:53:13+00:00Added an answer on June 13, 2026 at 5:53 pm

    You could create a dictionary that maps interfaces to whichever type you think should be the default (“default type for an interface” isn’t a defined concept in the language):

    var defaultTypeFor = new Dictionary<Type, Type>();
    defaultTypeFor[typeof(IList<>)] = typeof(List<>);
    ...
    var type = property.PropertyType;
    if (type.IsInterface) {
        // TODO: Throw an exception if the type doesn't exist in the dictionary
        if (type.IsGenericType) {
            type = defaultTypeFor[property.PropertyType.GetGenericTypeDefinition()];
            type = type.MakeGenericType(property.PropertyType.GetGenericArguments());
        }
        else {
            type = defaultTypeFor[property.PropertyType];
        }
    }
    theList = Activator.CreateInstance(type);
    

    (I haven’t tried this code; let me know if you get problems with it.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for some pointers on implementing Custom Events in VB.NET (Visual Studio 2008,
Implementing custom DataAnnotationsModelMetadataProvider in ASP.NET MVC2. Assuming the object that is being rendered looks
Implementing a custom membership provider, there are certain properties such as MinRequiredPasswordLength that only
I'm implementing a custom control that inherits from Control . I want it to
I'm implementing a custom control with some labels on it and I need to
I'm implementing a custom TextView and I want to do some action when the
I am implementing a custom collection implementation that can either be readonly or non-readonly;
Fixed: See notes at bottom I am implementing a generic class that supports two
I'm using ASP.NET MVC and implementing custom validation via custom attributes/data annotations on my
Implementing a custom Dependency Property on a Framework Element object causes my Visual Studio

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.