Search code examples
c#visual-studiogenerated-code

is there any easy way to auto generated code like this?


i have a file which will be used across many app projects. the only difference of these files is the webservice reference name. code like this:

public void Test(){
    Kevin.ServiceReference1.Service1Client client = new Kevin.ServiceReference1.Service1Client();
    // do something....
}

like code above, the 'Kevin.ServiceReference1' will be replace by specified app project namespace. so, according to DRY(don't repeat yourself), i shouldn't just copy the file to many projects and rename the specified part manually. is there any way i can easily replace some parts of my template file to something related to the project?


Solution

  • Have you looked into Microsoft's T4 code templating system ? It might be just what you need.

    Links: http://msdn.microsoft.com/en-us/vstudio/cc308634.aspx
    http://visualstudiomagazine.com/articles/2009/05/01/visual-studios-t4-code-generation.aspx