Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you really want to go down this route? Code generation will work.</p> <p><a href="http://msdn.microsoft.com/en-us/library/vstudio/bb126445.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/vstudio/bb126445.aspx</a></p> <p>Microsoft has embedded the T4 templating language into Visual Studio. This templating language allows quick and easy ways to generate boilerplate code. While the system itself is primitive, clumsy, and generally frustrating, it allows you to generate code with whatever approach you like.</p> <p>To do the basics, you'd make a template file describing your reusable code and logic.</p> <p>So for example, we could have a TemplatedFields.Include.tt file that looks like this</p> <pre><code>&lt;# // myFields and myClassName must be defined before importing this template #&gt; &lt;# // stuff in these braces will not appear in the outputted file, but are executed by the templating engine #&gt; //this code is outside of the braces and will appear in the file1 public partial class &lt;#= myClassName #&gt; //notice the equals sign. Works like webforms. { &lt;# ForEach(var field in myFields) { #&gt; private string _&lt;#= field.Name #&gt; = null; public string &lt;#= CapitalizeFirstLetter(field.Name) #&gt; { get { _&lt;#= field.Name #&gt; = GetLang(&lt;#= field.FirstParam #&gt;, "&lt;#= field.SecondParam #&gt;"); return _&lt;#= field.Name #&gt;; } set { if (object.Equals(value, _&lt;#= field.Name #&gt;)) return; SetLang(&lt;#= field.FirstParam #&gt;, "&lt;#= field.SecondParam #&gt;", value); OnPropertyChanged(); } } &lt;# } #&gt; } </code></pre> <p>And then for the definition of your... well, let's say this is Person.cs</p> <p>Person.Templated.tt</p> <pre><code>&lt;#@ output extension=".cs" #&gt; //stuff inside the angle braces is sent to the TT engine and does not appear in the file. &lt;# var myClassName = "Person"; var myFields = new List&lt;Field&gt;() { new Field {Name="Description", FirstParam="this.TabAccountLangs", SecondParam="TextAccount"), new Field {Name="Name", FirstParam="this.TabAccountLangs", SecondParam="TextAccount"), new Field {Name="MoarFieldzzzz", FirstParam="this.TabAccountLangs", SecondParam="TextAccount"), } #&gt; //included code is appears below, now that values have been set above. &lt;#@ include file="TemplatedFields.Include.tt" #&gt; </code></pre> <p>Saving the above file will generate Person.Templated.cs automatically. I don't remember if you need a directive or not to make sure that VS will compile the resulting CS file, but I'm pretty sure it does by default.</p> <p>I leave the implementation of CapitalizeFirstLetter and the definition of Field as an excersize for the reader. This is, of course, an exceptionally crude approach - there are far more structured and intelligent ways to build a framework with t4.</p> <p>Because the class is partial, you can provide more specific hand-coded logic in a second Person.cs file.</p> <p>Oleg Sych has made the t4toolbox to make large, complicated T4 projects easier, but I warn you: T4 are a path to madness.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload