Tuesday, December 19, 2017

T4 Generation Templates VS 2017


It took me a lot of time to find good resources about T4 (even if there are no real alternatives built-in for visual studio). Here a small template to create multiple files easily...

I used following resources exists using archive.org:

Further resources:


template debug="false" hostspecific="true" language="C#" #><#@

assembly name="System.Core" #><#@

import namespace="System.Linq" #><#@
import namespace="System.Text" #><#@
import namespace="System.IO" #><#@
import namespace="System.Collections.Generic" #><#@

output extension=".log" #><#

string propertyName = "Prop";
Enumerable.Range(1,3).ToList().ForEach(id => {

StringBuilder builder = new StringBuilder();
builder.AppendLine("public class Data" + id.ToString("00"));
builder.AppendLine(" {");
Enumerable.Range(1,3).ToList().ForEach(propId => {
builder.AppendLine(" public int "+propertyName+propId+" { get; set; }");
builder.AppendLine(" }");

CreateClass("Data"+id.ToString("00")+".cs", builder.ToString());

public void CleanOutputFolder()
string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
string outputFolder = Path.Combine(templateDirectory, "output/");
foreach(var file in Directory.GetFiles(outputFolder))

public void SaveOutput(string outputFileName)
      string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
      string outputFilePath = Path.Combine(templateDirectory, "output/", outputFileName);
      File.WriteAllText(outputFilePath, this.GenerationEnvironment.ToString());

      this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);

public void CreateClass(string fileName, string content)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
<#= content #>


Monday, December 18, 2017

Dumping objects in c#


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...


... 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;


#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