Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate code based which populate an object

Let's say I have the following classes.

public class MyClass {
   public string Data1 { get; set; }
   public MyOtherClass Data2 { get; set; }
   // 50+ other properties...
}

public class MyOtherClass {
   public string OtherData1 { get; set; }
   // More properties
}

There's somecode that instanciate that class and populates it with all the data. I'd like to use that object for my test. I could simply serialize the structure into XML and reload it later. However, what I would really like is to have the entire object tree build in the code. In other words:

MyClass myClass = new MyClass {
    Data1 = "Hello",
    Data2 = new MyOtherClass {
        OtherData1 = "World",
        // More...
    },
    // More...
}

I could write all that myself, but it would take hours and be error prone since there's a high number of properties and sub-classes. Here's my question: given an object how would you generate the code which populate that object?

like image 587
Martin Avatar asked Jul 19 '12 16:07

Martin


1 Answers

I would write a T4 template. Check out an example that is doing something, although really remotely, similar to what you need.

like image 65
Nikola Anusev Avatar answered Oct 16 '22 19:10

Nikola Anusev