Visual Studio中架构代码生成 - 创建列表<&型GT;而不是IEnumerable的<&型GT; [英] Visual Studio Architecture Code Generation - Create List<type> rather than IEnumerable<type>
问题描述
是否有可能使它创建与类型的属性时,从它生成的代码配置一个协会在一个Visual Studio(2013年)类图列表< MyClass的>
甚至的ICollection< MyClass的>
,而不是它的的IEnumerable<的默认; MyClass的>
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>
?
推荐答案
是的,这是可以改变的输出。 Visual Studio中使用T4模板从架构工具生成代码。
Yes, it is possible to change the output. Visual Studio uses T4 templates to generate code from the Architecture tools.
您可以找到在C模板:\Program文件(x86)\Microsoft的Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\建筑Tools\Extensibility\Templates\Text(删除(86)如果您有一个32位机)。
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).
使用以下步骤生成的代码更改为的IList< T>
而不是默认的的IEnumerable< T>
:
Use the following steps to change the generated code to IList<T>
instead of the default IEnumerable<T>
:
- 备份到你的计算机上的不同目录中的所有模板(最好是安全比抱歉)
- 从上面的目录
-
找到名为
的ElementType方法(ITYPE打开CSharpHelper.t4型,布尔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;
}
更改字符串的IEnumerable到任何你想要的(见我的评论开始与SO)
Change the string IEnumerable to whatever you want (see my comment starting with SO)
您甚至可以编写自己的T4模板和指导的Visual Studio生成代码时使用它们,更多的细节上的 MSDN 。
You can even write your own T4 Templates and instruct visual studio to use them when generating code, more details on MSDN.
这篇关于Visual Studio中架构代码生成 - 创建列表<&型GT;而不是IEnumerable的<&型GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!