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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:46:07+00:00 2026-05-31T11:46:07+00:00

I’m writing my own method to convert an object graph to a custom object

  • 0

I’m writing my own method to convert an object graph to a custom object since the JavaScriptSerializer fires errors on null values.

So this is what I have so far:

internal static T ParseObjectGraph<T>(Dictionary<string, object> oGraph)
{
    T generic = (T)Activator.CreateInstance<T>();
    Type resType = typeof(T);
    foreach (PropertyInfo pi in resType.GetProperties())
    {
        object outObj = new object();
        if (oGraph.TryGetValue(pi.Name.ToLower(), out outObj))
        {
            Type outType = outObj.GetType();
            if (outType == pi.PropertyType)
            {                        
                pi.SetValue(generic, outObj, null);
            }
        }
    }
    return generic;
}

Now the pi.SetValue() method runs, and doesn’t fire an error but when I look at the properties of generic, it’s still the same as it was before hand.

The first property it goes through is a boolean so the values end up like this

generic = an object of type MyCustomType
generic.property = false
outObj = true
pi = boolean property
outType = boolean

Then after the SetValue method runs, generic.property is still set to false.

  • 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-31T11:46:09+00:00Added an answer on May 31, 2026 at 11:46 am

    So I took your method and made a unit test of it:

    class PropertySetTest
    {
        static readonly Type resType = typeof(Car);
        internal static T ParseObjectGraph<T>(Dictionary<string, object> oGraph)
        {
            T generic = (T)Activator.CreateInstance<T>();
            foreach (PropertyInfo pi in resType.GetProperties())
            {
                //No need to new() this
                object outObj; // = new object();
                if (oGraph.TryGetValue(pi.Name.ToLower(), out outObj))
                {
                    Type outType = outObj.GetType();
                    if (outType == pi.PropertyType)
                    {
                        pi.SetValue(generic, outObj, null);
                    }
                }
            }
            return generic;
        }
    
        [Test]
        public void Test()
        {
            var typeData = new Dictionary<String, Object> {{"color", "Blue"}};
            var myCar = ParseObjectGraph<Car>(typeData);
            Assert.AreEqual("Blue", myCar.Color);
        }
    }
    
    internal class Car
    {
        public String Color { get; set; }
    }
    

    This passes. Can you make it not pass in the way that you are seeing?

    EDIT: With your struct, it is only marginally more complicated. See Jon Skeet’s answer here regarding what’s going on. As for the working code:

    class PropertySetTest
    {
        static readonly Type resType = typeof(Car);
        internal static T ParseObjectGraph<T>(Dictionary<string, object> oGraph)
        {
            Object generic = Activator.CreateInstance<T>();
            foreach (var pi in resType.GetProperties())
            {
                //No need to new() this
                object outObj; // = new object();
                if (oGraph.TryGetValue(pi.Name.ToLower(), out outObj))
                {
                    var outType = outObj.GetType();
                    if (outType == pi.PropertyType)
                        pi.SetValue(generic, outObj, null);
                }
            }
            return (T)generic;
        }
    
        [Test]
        public void Test()
        {
            var typeData = new Dictionary<String, Object> {{"color", "Blue"}};
            var myCar = ParseObjectGraph<Car>(typeData);
            Assert.AreEqual("Blue", myCar.Color);
        }
    }
    
    internal struct Car
    {
        public String Color { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string
I am writing an app with both english and french support. The app requests

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.