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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:11:01+00:00 2026-06-18T07:11:01+00:00

I am working on the Reflection , but i am stuck while doing the

  • 0

I am working on the Reflection , but i am stuck while doing the recursion.

Code :

public class User {
  public string Name;
  public int Number;
  public Address Address;    
}


public class Address {
 public string Street;
 public string State;
 public string Country;
}

now i am printing the values.

 Type t = user.GetType();  
 PropertyInfo[] props = t.GetProperties(); 
 foreach (PropertyInfo prp in props)  
 {  
   if(!prp.GetType().IsPrimitive && prp.GetType().IsClass) 
   {
     // Get the values of the Inner Class.
     // i am stucked over here , can anyone help me with this.

           Type ty = prp.GetType();
           var prpI = ty.GetProperties();
           //var tp = ty.GetType().;
            foreach (var propertyInfo in prpI)
            {
            var value = propertyInfo.GetValue(prp);
            var stringValue = (value != null) ? value.ToString() : "";
            console.WriteLine(prp.GetType().Name + "." + propertyInfo.Name+" Value : " +stringValue);    
            }
   }
   else
   {    
     var value = prp.GetValue(user);   
     var stringValue = (value != null) ? value.ToString() : "";
     console.writeline(user.GetType().Name + "." + prp.Name+" Value : " +stringValue); 
   }
 }

i want to know how to find out whether the property is a class or the primitive. and do the recursion if it is a class.

  • 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-18T07:11:02+00:00Added an answer on June 18, 2026 at 7:11 am

    First of all, if you want to access the properties of a type, ensure you are using a type which has properties:

    public class User {
      public string Name{get;set;}
      public int Number{get;set;}
      public Address Address{get;set;}    
    }
    
    
    public class Address {
     public string Street{get;set;}
     public string State{get;set;}
     public string Country{get;set;}
    }
    

    Second, prp.GetType() will always return PropertyInfo. You are looking for prp.PropertyType, which will return the Type of the property.

    Also, if(!prp.GetType().IsPrimitive && prp.GetType().IsClass) won’t work the way you want, because String e.g. is a class and also not a primitive. Better use prp.PropertyType.Module.ScopeName != "CommonLanguageRuntimeLibrary".

    Last but not least, to use recursion, you actually have to put your code into a method.

    Here’s a complete example:

    IEnumerable<string> GetPropertInfos(object o, string parent=null)
    {
        Type t = o.GetType();  
        PropertyInfo[] props = t.GetProperties(BindingFlags.Public|BindingFlags.Instance);
        foreach (PropertyInfo prp in props)  
        {  
            if(prp.PropertyType.Module.ScopeName != "CommonLanguageRuntimeLibrary")
            {
                // fix me: you have to pass parent + "." + t.Name instead of t.Name if parent != null
                foreach(var info in GetPropertInfos(prp.GetValue(o), t.Name))
                    yield return info; 
            }
            else
            {    
                var value = prp.GetValue(o);   
                var stringValue = (value != null) ? value.ToString() : "";
                var info = t.Name + "." + prp.Name + ": " + stringValue;
                if (String.IsNullOrWhiteSpace(parent))
                    yield return info; 
                else
                    yield return parent + "." + info; 
            }
        }
    }
    

    Used like this:

    var user = new User { Name = "Foo", Number = 19, Address = new Address{ Street="MyStreet", State="MyState",  Country="SomeCountry" }    };
    foreach(var info in GetPropertInfos(user))
        Console.WriteLine(info);
    

    it will output

    User.Name: Foo
    User.Number: 19
    User.Address.Street: MyStreet
    User.Address.State: MyState
    User.Address.Country: SomeCountry
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am recrusively evaluating properties via reflection using object.GetType().GetProperty(string propertyName). This is working fine
I found interesting thing while working with reflection. I tried to retrieve constructors of
I'm working on a reflection project, and now I'm stuck. If I have an
This week I've been working on some reflection-based code, and during unit testing found
While working with reflection, I recently got to the point where I wanted to
I'm currently working on a problem that involves System.Reflection.Emit code generation. I'm trying to
Support for reflection has been currently added into F#, but it is not working
Working with Asp.Net MVC for quite some time now, but I am stuck on
OK, I've been working on something for a while now, using reflection to accomplish
I'm trying to make if-else working in IL by System.Reflection and System.Reflection.Emit. This is

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.