Monday, December 18, 2017

Dumping objects in c#

Hi,

I had a look at different possibilities to .net objects. First I was like: "just serialize that stuff and everything will be fine", but the more I thought about it, the more I realized that I needed more...

First of all: LinqPad did a great job with its functionality to dump objects, which was very inspiring to me...

see:



... so what might be a good solution to embed into our code...?

Good fitting solution seemed to be ObjectDumper (unfortunately you can find different solutions with the same name in google):

ObjectDumper (version: stone-age)



ObjectDumper (version: old)



ObjectDumper (not a single-file-solution anymore, Options to ignore fields)



ObjectDumper (Finally the current version without a single commit in the last 2 years... same version as CodePlex?)



So I started further testing with the github version:


  • installation: copy Dumper.cs and DumperOptions
  • DumperOptions can be removed easily... only 1 line must be patched (the rest is just handover of the options object into the recursive call)


IEnumerable<FieldInfo> fields = XY ? Enumerable.Empty<FieldInfo>() : type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

you can replace XY by a constant or a static field, so even the github version can be a single file solution again.

  • alternatively nuget is available (as already mentioned)
  • its a simple recursive solution based on GetFields and GetProperties
  • can read private data using reflection but can not handle static members
  • Every reference type is pushed into an object of class ObjectIDGenerator generating making it easy to make cross-references of objects (e.g.: backing field and property)
    • not a feature I really need, so I tried to delete it, but found out that it is a perfect solution to remove circular stepping into the code, so it is not only for printing purpose.
For me it works perfectly.

The following Test shows a first impression:

        public class Data
        {
            private int secret = 15;
            private string secondSecret = "15";
            public static string staticData = "123";
            public int Public => secret * 2 + 3;
            public string Something => "something";
            public int StoredData { get; set; }
            public string StoredData2 { get; set; }
            public Data()
            {
                this.StoredData = 1;
                this.StoredData2 = "1";
            }
            public Data2 data2 = new Data2();
            public class Data2
            {
                public bool IsData { get; set; } = false;
            }
        }

Dumped:

#1: data [DumpTester.Program+Data]
{
   properties {
      Public = 33 [System.Int32]
      #2: Something = "something" [System.String]
      StoredData = 1 [System.Int32]
      #3: StoredData2 = "1" [System.String]
   }
   fields {
      secret = 15 [System.Int32]
      #4: secondSecret = "15" [System.String]
      <StoredData>k__BackingField = 1 [System.Int32]
      <StoredData2>k__BackingField = "1" [System.String] (see #3)
      #5: data2 [DumpTester.Program+Data+Data2]
      {
         properties {
            IsData = False [System.Boolean]
         }
         fields {
            <IsData>k__BackingField = False [System.Boolean]
         }
      }
   }
}
you see:
  • no static info
  • AutoProperties with Backing-Fields
  • For reference-auto-properties you see the linkage between Prop and Field (see #3)
  • ValueTypes have no reference
  • Types in the system namespace are not dumped


kr,
Daniel

No comments: