1

I have an object type of e.g.:

Class
{
  public string Variable {get; set;}
  public List<AnotherClass> ListVariable {get; set;}
}

AnotherClass
{
  public string Variable {get; set;}
  public int IntVariable {get; set;}
}

I've tried several solutions (ObjectDumper, object.GetProperties) to print all the Class object values to screen; The problem concludes in inability to print List<AnotherClass>. Instead of all it's items I get only it's count property.

Tried solutions:

How to recursively print the values of an object's properties using reflection

Recursively Get Properties & Child Properties Of An Object

Finding all properties and subproperties of an object

and several more..

EDIT:

Ok, as I see, I probably didn't describe the problem well. I need to print all the object's properties and their values, when I don't know type of the object and it's properties. The listed solutions work fine, if object contains only simple properties. The problem shows up if one of the properties is List<>

I tried the following:

1)

private static void PrintObject(Object dataSource)
{
   foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(dataSource))
   {
      string name = descriptor.Name;
      object value = descriptor.GetValue(dataSource);

      RichTextBox.Text += name + ": " + value + "\n";

      PrintObject(control, value);
   }
}

Gives me the output:

CheckStatus: Performed

TextData: System.Collections.Generic.List`1[TextDataField]

Capacity: 16

Count: 15

but I was expecting all 15 item values here, not just list count.

2)

RichTextBox.Text = dataSource.DumpToString(); 

from http://objectdumper.codeplex.com/

Gives pretty much the same output.

Community
  • 1
  • 1
insomnium_
  • 1,800
  • 4
  • 23
  • 39
  • 3
    Show the code where you print it. – CodeCaster Jun 14 '13 at 06:37
  • `foreach(var listItem in listVariable)`?.. or did you mean [GetFields](http://msdn.microsoft.com/en-us/library/ch9714z3.aspx) – Sayse Jun 14 '13 at 06:39
  • 1
    This question is perfectly fine. But to tell you, this is already discussed (may be reason for downvotes). See any of the questions http://stackoverflow.com/questions/852181/c-printing-all-properties-of-an-object, http://stackoverflow.com/questions/360277/what-is-the-best-way-to-dump-entire-objects-to-a-log-in-c, http://stackoverflow.com/questions/1347375/c-sharp-object-dumper. All the answers there take care of collection types as well. In case you have tried them and you still have problem, then post what you tried, and the class and where the problem occurred and what occurred.. – nawfal Jun 14 '13 at 07:24
  • I know it was.. But somehow the solutions I've tried did not work for me, when object contains List. – insomnium_ Jun 14 '13 at 07:41
  • 1
    are they fields or properties? – aiapatag Jun 14 '13 at 07:53
  • public properties with getters and setters. Sorry not mentioning it in question. – insomnium_ Jun 14 '13 at 07:54
  • 1
    @insomnium_ they dont work because those libraries are not meant for it. The ones u have used gives the expected result. They dont treat collection types specially. But the good news is there are plenty of other ones that helps you here. You should go for such a library (in all the three links I gave you there are such classes). See for an example: http://stackoverflow.com/a/10478008/661933. Or this http://stackoverflow.com/a/3514115/661933, or http://stackoverflow.com/a/1347630/661933 – nawfal Jun 14 '13 at 08:36

4 Answers4

0

If you don't know type of the object and it's properties, give the class (AnotherClass) his own Method for Print:

public Class
{
    public string variable;
    public List<AnotherClass> listVariable;

    public void Print()
    {
        foreach(var item in listVariable)
        {
           // ...or how you what to print?
           Console.WriteLine(item.GetString());
        }
    }
}

public AnotherClass
{
    public string variable;
    public int intVariable;

    public string GetString()
    {
        // Format here your output.
        return String.Format("Var: {0}, IntVar: {1}", this.variable, this.intVariable);
    }
}
0

"dumper" methods are the easiest and the most correct way of doing it, when you are in control of the source of the classes you want to look at. It's also the safest method because you can dump non-public stuff while preventing direct access to outsiders.

Simplest way to achieve this is to override the ToString() method which Object provides to any type. This way, your code will be clean and readable.

You might also do it through System.Reflection but it would be more complicated and provide no additional benefit. KISS is the keyword here.

Like this (tested in LINQPad): NOTE public stuff is supposed to be capitalized, like this: public int Variable

void Main()
{
    Class myClassObj = new Class();
    myClassObj.Variable = 1;
    myClassObj.ListVariable = new List<AnotherClass>();
    myClassObj.ListVariable.Add(new AnotherClass{ Variable="some", IntVariable=2 });

    Console.WriteLine(myClassObj.ToString());
}

public class Class
{
    public int Variable{get;set;}
    public List<AnotherClass> ListVariable {get;set;}

    public override string ToString()
    {
        return string.Join(", ", new string[]{ 
            string.Format("Variable= {0}", + this.Variable),
            string.Format("ListVariable= [{0}]", string.Join(", ", this.ListVariable))
        });
    }
}

public class AnotherClass
{
    public string Variable{get;set;}
    public int IntVariable{get;set;}

    public override string ToString()
    {
        return string.Join(", ", new string[]{
            string.Format("Variable={0}", this.Variable),
            string.Format("IntVariable={0}", this.IntVariable)
        });
    }
}
Alex
  • 23,004
  • 4
  • 39
  • 73
  • 1
    I, actually, have tons of possible object type classes. Those I've listed in question are just example, so this solution involves a lot of code writing to be done. Thanks anyway! – insomnium_ Jun 14 '13 at 07:16
-1

You could serialize it as JSON using the JavaScriptSerializer class:

var serializer = new JavaScriptSerializer();
string dump = serializer.Serialize(yourObjectInstance);
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
-1

I guess this may help you.

Class A
    {
      public string variable;
      public List<B> listVariable;
    }

Class B
    {
      public string variable;
      public int intVariable;
    }

A obj = new A();  
//obj.listVariable[index].variable;
//obj.listVariable[index].intVariable;
Praveen
  • 55,303
  • 33
  • 133
  • 164