1

I have huge C# classes defined with many fields/variables. Each variable could be a simple datatype or a list or another class ....

I want to dump all variables and their datatype iteratively going thru child classes as well. Is there any simple C# function/code to do this?

PS: I am not looking at run time object values. Just extract of name and datatype is enough.

RK

user1912826
  • 25
  • 1
  • 3
  • 2
    You seem to know what to do, since you've added the reflection tag. Have you tried anything so far? – Geeky Guy Aug 12 '13 at 21:33
  • right click the solution and choose class diagram? – Sayse Aug 12 '13 at 21:33
  • _"I have huge C# classes"_. Wouldn't it be better to use a single `Dictionary` instead? The key is the name of the property/field (or even better: an enum) and the value is the value of the property. – Tim Schmelter Aug 12 '13 at 21:36
  • How about using DotPeek from JetBrains Or must this be c# code? http://www.jetbrains.com/decompiler/index.html?topDP – phillip Aug 12 '13 at 21:36
  • if you want code then asked and answered here: http://stackoverflow.com/questions/360277/what-is-the-best-way-to-dump-entire-objects-to-a-log-in-c and here: http://stackoverflow.com/questions/1347375/c-sharp-object-dumper – phillip Aug 12 '13 at 21:41
  • The reflection samples I checked do mainly for objects. If something is not initialized they wont return. I am mainly interested in structure of classes. Primary goal is to create DB tables based on output columns (1 variable to 1 column in DB) – user1912826 Aug 12 '13 at 22:09

1 Answers1

0

You could try to create the following setup. The GetData class will take as its single parameter the assembly, that you want to scan for different types. This class will then take all the classes and their child classes it can find, together with their fields and create a structured XML file which will hold that data.

class GetData
{
    public GetData(Assembly assembly)
    {
        var xml = new XDocument(new XElement("root"));

        foreach (var type in assembly.GetTypes())
        {
            var typeElement = new XElement("Class", new XElement("Name", type.Name));

            foreach (var field in type.GetFields())
            {
                typeElement.Add(new XElement("Field",
                    new XElement("Name", field.Name),
                    new XElement("Type", field.FieldType)));
            }

            xml.Root.Add(typeElement);
        }

        Console.WriteLine(xml);

        xml.Save("Dump.xml");
    }
}

You can create this test class with an inner class so that you can be sure it will even work on this kind of setup.

public class DbClass
{
    public int ID;
    public string Name;

    public class DbChildClass
    {
        public int ChildID;
        public string Company;
    }
}

To get all the types from your currently executing assembly, simply execute the following lines of code.

class Program
{
    static void Main()
    {
        new GetData(Assembly.GetExecutingAssembly());

        Console.Read();
    }
}
Mario Stopfer
  • 541
  • 3
  • 8