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 1037089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:48:39+00:00 2026-05-16T14:48:39+00:00

I am attempting to create a generic mapping function that takes the values of

  • 0

I am attempting to create a generic mapping function that takes the values of a dictionary and its sub classes and maps them to the fields in T using reflection. Within object T, there are sub objects that must be drilled into, and through recursion, it seems like a pretty simple concept. However I am stuck — and I’m not sure if it’s a limitation of generics, or if it’s something I am simply doing wrong. Here’s my function:

I call the first instance with the following.

OrderDetails readyOrder = Tools.MapStruct<OrderDetails>(order);

*XmlRpcStruct is a dictionary, and the sub classes are always XmlRpcStruct’s — which conta

    public static T MapStruct<T>(XmlRpcStruct xmlRpcStruct) where T : new()
    {
        T map = new T();
        foreach (DictionaryEntry entry in xmlRpcStruct)
        {
            XmlRpcStruct entryStruct = (XmlRpcStruct)entry.Value;
            foreach (DictionaryEntry subEntry in entryStruct)
            {
                if (subEntry.Value.GetType() != typeof(XmlRpcStruct))
                {
                    var prop = map.GetType().GetField(subEntry.Key.ToString());
                    prop.SetValue(map, subEntry.Value);
                }
                else
                {
                    var prop = map.GetType().GetField(subEntry.Key.ToString());
                    ERROR -->prop.SetValue(map, MapStruct<prop.GetType()> (subEntry.Value));
                }
            }
        }
        return map;
    }

For example, I could have a dictionary with the following data:

Key—-Value

First–John

Last—Smith

Age—-45

Address-Dictionary Object

…and an object:

obj.First (string)

obj.Last (string)

obj.Age (int)

obj.Address (AddressType)

I’m using the type in the object to determine what the Dictionary object from the name value pair should be cast as.

I need to be able to dynamically get the type of the sub item within my dictionary and recursively call the same function. Where am I going wrong?

  • 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-05-16T14:48:40+00:00Added an answer on May 16, 2026 at 2:48 pm

    Why not wrap it? It’s using reflection anyway…

    public static T MapStruct<T>(XmlRpcStruct xmlRpcStruct) where T : class, new()
    {
        return (T)MapStructInternal(typeof(T), xmlRpcStruct);
    }
    
    private static object MapStructInternal(Type t, XmlRpcStruct xmlRpcStruct)
    {
        object map = t.GetConstructor(new Type[0] ).Invoke(new object[0]);
        foreach (DictionaryEntry entry in xmlRpcStruct)
        {
            XmlRpcStruct entryStruct = (XmlRpcStruct)entry.Value;
            foreach (DictionaryEntry subEntry in entryStruct)
            {
                if (subEntry.Value.GetType() != typeof(XmlRpcStruct))
                {
                    var prop = map.GetType().GetField(subEntry.Key.ToString());
                    prop.SetValue(map, subEntry.Value);
                }
                else
                {
                    var prop = map.GetType().GetField(subEntry.Key.ToString());
                    prop.SetValue(map, MapStructInternal(map.GetType(),(XmlRpcStruct)subEntry.Value));
                }
            }
        }
        return map;
    }    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to create an Expression that will invoke a specific generic overloaded
I am trying to create a generic click event function for all the fields
I'm attempting to create a generic controller, ie: public class MyController<T> : Controller where
I am attempting to create a report, utilizing a matrix, that only displays columns
I'm attempting to create a single Controller class to handle all foreseeable surveys that
I'm attempting to create a Parcelable class in Android so that I can pass
I'm attempting to create a recursive routine that will retrieve PropertyInfos for all members
I am attempting to create a Dictionary from my DataTable. Currently i do this
I'm attempting to create a custom control that inherits from System.Web.UI.WebControls.ListView . Codebehind: using
I'm attempting to make a generic routine within my program that will instantiate objects

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.