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

  • Home
  • SEARCH
  • 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 8775555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:49:46+00:00 2026-06-13T18:49:46+00:00

I am having an issue trying to recursively use a generic function for a

  • 0

I am having an issue trying to recursively use a generic function for a deserialization algorithm. I am loading in the variables from an XElement by their value. However, if they are no a primitive, I have to recursively do the same operation on that object. However, that object is not the same type as the current one being processed. So for instance if I have

 class someObject
 {
      otherObject obj = new otherObject();
 }

 class otherObject
 {
     int someInt = 123;
 }

the deserializer recursively goes into the other object. However, the issue is that I have to change the type of the generic method dynamically (ie. I do not know what type it will be at compile time). The algorithm is as follows:

public static T deserialize<T>(XNode element)
    where T : new()
{
    XElement currentNode = (XElement)element;

    FieldInfo[] fields = getFields<T>();

    T returnValue = new T();

    foreach (FieldInfo field in fields)
    {
        if (field.FieldType.IsPrimitive)
        {
            field.SetValue(returnValue, currentNode.Element(field.Name).Value);
        }
        else
        {
            //The issue is on the following line
            field.SetValue(returnValue, deserialize<???>(currentNode.Element(field.Name).Value));
        }
    }
}

I have seen plenty of documentation on how to dynamically create typed lists and objects. However, I cannot find anything that deals with dynamically switching the type on a generic method call.

Any help is greatly appreciated! Let me know if am not being clear…

  • 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-13T18:49:48+00:00Added an answer on June 13, 2026 at 6:49 pm

    I would make the generic method call another private non-generic method that uses a different means of creating the instance instead of new T(). The outer generic method will be convenient for a caller of the method, so they don’t have to cast the response. Internally, your code will be simpler.

    Note that the .NET framework’s deserialisation methods all at least have overloads that take a Type instance instead of a generic type parameter (<T>). This is for the reason you’re discovering — sometimes you have the type as an object, and then you can’t (sensibly) call a generic method and pass that type parameter.

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

Sidebar

Related Questions

I'm having this issue when trying to use an ASPX page from a referenced
I am having a big issue trying to delete from an UITableView. Just for
I'm having an issue trying to call a web service from stored procedure. sp_OACreate
I am having an issue trying to trigger the test() function when updating the
I am having an issue trying to create a re-useable function for setting up
I'm having a issue trying to pass the data from my SQLite DB to
I'm new to RoR and having an issue when trying to save from multiple
I'm having an issue trying to NOT select a tablerow with the function: $(tr).click(function(e)
I am having an issue while trying to use a GridView in a PopupWindow.
I'm having an issue with trying to use VB.NET and the WebRequest object to

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.