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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:43:05+00:00 2026-05-31T23:43:05+00:00

I have to convert a generic object(s) into a NameValueCollection. I am attempting to

  • 0

I have to convert a generic object(s) into a NameValueCollection. I am attempting to use reflection. While I can get the parent properties, I cannot get the properties of a parent property that is an object.

Class One 
public string name{get;set}

Class Two
public string desc{get;set}
public One OneName{get;set;}

public static NameValueCollection GetPropertyName(
        string objType, object objectItem)
{
    Type type = Type.GetType(objType);
    PropertyInfo[] propertyInfos = type.GetProperties();
    NameValueCollection propNames = new NameValueCollection();

    foreach (PropertyInfo propertyInfo in objectItem.GetType().GetProperties())
    {
        if (propertyInfo.CanRead)
        {
            var pName = propertyInfo.Name;
            var pValue = propertyInfo.GetValue(objectItem, null);
            if (pValue != null)
            {
                propNames.Add(pName, pValue.ToString());
            }
        }
    }

    return propNames;
}

I assume there has to be somesort of recursive call but cannot figure out how to do it. Any help is appreciated.

  • 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-31T23:43:06+00:00Added an answer on May 31, 2026 at 11:43 pm

    I will assume that you want resulting NameValueCollection to contain properties from both Class One and Class Two when the input is of type Class Two.

    Now that we have that established, what you can do is to examine a type of each property.
    If it’s one of built-in types (Type.IsPrimitive() can help you determine that), you can add the property to resulting NameValueCollection right away. Otherwise, you need to go over each property of that non-primitive type and repeat the process for it again. As you mention, here is the place for recursion.

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

Sidebar

Related Questions

I get an error of Cannot convert object of type System.String' to type 'System.Collections.Generic.List'1[...+ContactHolder]'
I have an object, want to convert the generic object to class object. I
I have a requirement where I can get the following in an object -
I have to convert an incoming String field into a BigDecimal field that would
I want to convert a string to a generic type I have this: string
I have a function that returns map value (String) as a generic Object. How
I have a type converter which converts a string into an object and vice
I have a method which takes (object value) and convert it to string with
I have a small C# function that takes any generic SQLDataReader and converts it
We have to convert EVERYTHING to images for archiving purpose. DOC, HTML, email, ZIP,

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.