方法或属性的 Visual Studio UML 2010 代码生成
我建立了一个 Visual Studio 2010 UML 建模项目,并创建了一个包含多个类的类图。代码生成工作正常(我正在使用 Visual Studio 的可视化和建模功能包中提供的代码生成模板)。 每当我在类上单击“生成代码”时,我都会得到一个很好的类存根,其中包括该类的属性和方法存根。
现在我尝试将一些自定义代码添加到类的方法中。这个想法是,每次我为类生成代码时,方法中都已经写入了一些代码(默认情况下,每个方法的内容是“抛出 new Sytem.NotImplementedException()”)。据我了解,我首先需要创建一个 tt / t4 模板。然后我需要在类设计器的方法属性中设置文本模板绑定 - 它们应该指向新创建的模板。我尝试过,但我不知道如何让它工作。或者我在这里误解了一个概念?有什么想法吗?
是的,我已经阅读了有关代码生成和文本模板的 msdn 文档,以及 Oleg Sych 的博客。但这些都没有告诉我解决这个问题的方法。
I set up a visual studio 2010 UML modeling project and created a class diagram with several classes. The code generation works fine (I am using the code generation template delivered within the Visualization and Modeling Feature Pack for Visual Studio).
Whenever I click "Generate Code" on a class, I get a nice class stub including properties and method stubs for this class.
Now I am trying to add some custom code into a method of a class. The idea is to have every time I generate the code for the class the method would already have some code written in it (By default, the content of each method is "throw new Sytem.NotImplementedException()"). As far as I understand I first need to create a tt / t4 template. Then I need to set up in the class designer in the properties of the method the text template bindings - they should point to the newly created template. I tried it, but I can't figure out how to get this working. Or do I misunderstand a concept here? Any ideas?
And yes, I have read the msdn documentation about code generation and text templates, as well as Oleg Sych's blog. But neither of these did show me a way to solve this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看这篇文章如何:从 UML 类图生成代码 :它表示您需要安装 Visual Studio 功能包才能在 UML 项中获取“生成代码”命令。据我所知,您需要订阅 MSDN 才能获取该功能包。所以我没能去尝试。
有一个标题为“自定义生成的代码命令”的部分,它确实建议与 UML 结合使用来修改默认的 T4 模板。
如果我是您并且没有选择获取 MSDN 订阅,我会编写一个外接程序(或 VS 扩展)并处理 UML 文件。这需要您了解 UML 文件的内部工作原理才能从 UML 生成 C#。
这是一些 *.classdiagram 文件的 UML。它适用于包含属性(字段)和操作的类。从这里手动启动是一种选择吗?
Check this article out How to: Generate Code from UML Class Diagrams: it says that you need to install a Visual Studio Feature Pack to get a Generate Code command in the UML items. You need an MSDN subscription to get that Feature Pack, as far as I can tell. So I couldn't try it out.
There is a section titled Customizing the Generated Code Command and it indeed suggests modifying the default T4 templates using in conjunction with the UML.
If I were you and I had no option to get the MSDN subscription I'd write an Add-in (or a VS extension) and process the UML file. That would require you to understand the inner working of the UML file to generate C# from UML.
Here's the UML of some *.classdiagram file. It is for a class that contains an attribute (field) and an operation. Is starting manually from here an option?