Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Architecture Code Generation - Create List<type> rather than IEnumerable<type>

Is it possible to configure an Association in a Visual Studio (2013) class diagram so that when the code is generated from it that it creates a property with type List<MyClass> or even ICollection<MyClass> rather than it's default of IEnumerable<MyClass>?

like image 362
Andrew Maggs Avatar asked May 11 '14 12:05

Andrew Maggs


People also ask

What is code generation in Visual Studio?

Applies to: Visual Studio Visual Studio for Mac Visual Studio Code Code generation allows you to produce program code that is strongly typed, and yet can be easily changed when the source model changes.

What kind of code can be examined in Visual Studio?

These tool windows can examine code in Visual Studio projects, .NET components, COM components, dynamic-link libraries (DLL), and type libraries (TLB).

What are the architecture and modeling tools available in Visual Studio?

Visual Studio is available in several editions. Not all of these provide support for the architecture and modeling tools. The following table shows the availability of each tool. Only supports reading code maps, filtering code maps, adding new generic nodes, and creating a new Directed Graph from a selection.

What is a source generator in Visual Studio?

A Source Generator is a .NET Standard 2.0 assembly that is loaded by the compiler along with any analyzers. It is usable in environments where .NET Standard components can be loaded and run. Now that you know what a Source Generator is, let’s go through some of the scenarios they can improve.


1 Answers

Yes, it is possible to change the output. Visual Studio uses T4 templates to generate code from the Architecture tools.

You can find the templates at C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Architecture Tools\Extensibility\Templates\Text (remove (x86) if you have a 32-bit machine).

Use the following steps to change the generated code to IList<T> instead of the default IEnumerable<T>:

  1. Back up all templates to a different directory on your machine (better be safe than sorry)
  2. Open CSharpHelper.t4 from the above directory
  3. Locate the method named ElementType(IType type, bool isEnumerable = false)

     private static string ElementType(IType type, bool isEnumerable = false)
    {
        string text = string.Empty;
        if (type == null)
        {
            text = "object";
        }
        else 
        {
            text = TypeName(type);
        }
    
        if(!string.IsNullOrWhiteSpace(text) && isEnumerable)
        {
           //SO Change IEnumerable to IList here
            text = "IEnumerable<" + text + ">";
        }
    
        return text;
    }
    
  4. Change the string IEnumerable to whatever you want (see my comment starting with SO)

  5. Save the T4 file and generate your code from visual studio

You can even write your own T4 Templates and instruct visual studio to use them when generating code, more details on MSDN.

like image 99
user3373870 Avatar answered Sep 28 '22 04:09

user3373870