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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:52:47+00:00 2026-05-23T17:52:47+00:00

Given the following example class: public class MyClass { public string S { get;

  • 0

Given the following example class:

public class MyClass
{
    public string S { get; set; }
    public int I { get; set; }
    public DateTime D { get; set; }
    private float F { get; set; }
    private long l;

    public MyClass()
    {
        S = "foo";
        I = 42;
        D = new DateTime(2011, 11, 11);
        F = 3.14f;
        l = 12435;
    }
}

If I in my application have an instance myClass of this class, step through the code in debug mode (Visual Studio 2010), and at some point types myClass into the Immediate Window, the following is displayed:

{MyClass}
    D: {11.11.2011 00:00:00}
    F: 3.14
    I: 42
    l: 12435
    S: "foo"

Getting such a string representation of the object and all its values could be very useful for logging purposes. Is there a nice and easy way to achieve this?

I guess the Immediate Window uses reflection to loop over all fields and properties, but I thought I’d ask just in case there already exists some utility function or anything to do it.

  • 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-23T17:52:47+00:00Added an answer on May 23, 2026 at 5:52 pm

    This will store all fields in a dictionary (ensuring they can be read first):

    public static Dictionary<string, object> GeneratePropertiesDictionary(object myClass)
    {
        return myClass.GetType()
                      .GetProperties()
                      .Where(p => p.CanRead)
                      .ToDictionary(p => p.Name, p => p.GetValue(myClass, null));
    }
    

    You could easily modify this to append each name/value to a StringBuilder object instead of a dictionary though, and dump that to a file.

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

Sidebar

Related Questions

Given the following code: [example.h] class MyClass { public: MyClass(); std::string name; std::string address;
Given the following source types: public class BaseViewModel { public string Prop1 { get;
Given the following code from a Microsoft example: public class EngineMeasurementCollection : Collection<EngineMeasurement> {
Given the following simple example: List<string> list = new List<string>() { One, Two, Three,
Say I have a class, something like the following; class MyClass { public: MyClass();
given the following code: public class MainFrame extends JFrame{ public MainFrame() throws HeadlessException {
Given the following template: template <typename T> class wrapper : public T {}; What
Given the following code, [DataContract] public class TestClass { [DataMember] public object _TestVariable; public
Given the following example, why do I have to explicitly use the statement b->A::DoSomething()
Given a sorted vector with a number of values, as in the following example:

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.