方法或属性的 Visual Studio UML 2010 代码生成

发布于 2024-12-23 03:59:51 字数 451 浏览 0 评论 0原文

我建立了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福%小乖 2024-12-30 03:59:51

查看这篇文章如何:从 UML 类图生成代码 :它表示您需要安装 Visual Studio 功能包才能在 UML 项中获取“生成代码”命令。据我所知,您需要订阅 MSDN 才能获取该功能包。所以我没能去尝试。

有一个标题为“自定义生成的代码命令”的部分,它确实建议与 UML 结合使用来修改默认的 T4 模板。
如果我是您并且没有选择获取 MSDN 订阅,我会编写一个外接程序(或 VS 扩展)并处理 UML 文件。这需要您了解 UML 文件的内部工作原理才能从 UML 生成 C#。

这是一些 *.classdiagram 文件的 UML。它适用于包含属性(字段)和操作的类。从这里手动启动是一种选择吗?

<?xml version="1.0" encoding="utf-8"?>
<logicalClassDesignerModel xmlns:dm0="http://schemas.microsoft.com/VisualStudio/2008/DslTools/Core" xmlns:dm1="http://schemas.microsoft.com/dsltools/Kernel" xmlns:dm2="http://schemas.microsoft.com/dsltools/Component" xmlns:dm3="http://schemas.microsoft.com/dsltools/Activity" xmlns:dm4="http://schemas.microsoft.com/dsltools/Interaction" xmlns:dm5="http://schemas.microsoft.com/dsltools/UseCase" xmlns:dm6="http://schemas.microsoft.com/dsltools/UmlModelLibrary" xmlns:dm7="http://schemas.microsoft.com/dsltools/UmlDiagrams" xmlns:dm8="http://schemas.microsoft.com/dsltools/ModelStore" dslVersion="1.0.0.0" Id="0b035e1e-cf78-4d7b-a9c5-7ae15e246642" name="UMLClassDiagram1" linkedPackageId="30fb2a44-daa7-48cf-a306-c91b92f1dd7b" xmlns="http://schemas.microsoft.com/dsltools/LogicalClassDesigner">
  <packagedElements>
    <logicalClassDesignerModelHasTypes>
      <class Id="5d701b74-ed36-4baa-8318-0406964414ce" name="Class1" isAbstract="false" isLeaf="false" isActiveClass="false">
        <elementDefinition Id="82f92ae7-5d9b-4e64-8497-39c686ce2e5d" />
        <ownedAttributesInternal>
          <property Id="06827ace-592e-4cf7-bc13-946f073f2b73" name="Attribute2" isLeaf="false" isStatic="false" isReadOnly="false" isDerived="false" isDerivedUnion="false" aggregation="None" isComposite="false">
            <elementDefinition Id="3ebbef23-6573-42bf-8542-a3504f1ef28a" />
          </property>
        </ownedAttributesInternal>
        <ownedOperationsInternal>
          <operation Id="c061b4ea-9880-4674-8775-a7b360a2791d" name="Operation1" isLeaf="false" isStatic="false" isAbstract="false" concurrency="Sequential" isQuery="false">
            <elementDefinition Id="fe154244-f792-415d-8ebc-50d5e3a13362" />
            <ownedParameters>
              <operationHasOwnedParameters>
                <parameter Id="6cbf9775-608c-4a1c-bd17-14397c5ce0f3" direction="Return">
                  <elementDefinition Id="f9eabe6e-f42c-4ece-a0fc-6da2a5da11f0" />
                  <type_NamedElement>
                    <referencedTypeMoniker Id="7acb8bf9-6778-42b4-949c-5fbb1ed0a9bc" LastKnownName="String" />
                  </type_NamedElement>
                </parameter>
              </operationHasOwnedParameters>
            </ownedParameters>
          </operation>
        </ownedOperationsInternal>
      </class>
    </logicalClassDesignerModelHasTypes>
    <packageHasNamedElement>
      <referencedType Id="7acb8bf9-6778-42b4-949c-5fbb1ed0a9bc" name="String" isAbstract="false" isLeaf="false" cachedFullName="String">
        <elementDefinition Id="59259974-6d55-42c6-b7bd-763d77ac8ef9" />
      </referencedType>
    </packageHasNamedElement>
  </packagedElements>
  <package Id="30fb2a44-daa7-48cf-a306-c91b92f1dd7b" name="ModelingProject1">
    <elementDefinition Id="7c93d198-9011-4ef0-b6ba-9bd438a05ad5" />
    <profileInstances>
      <packageHasProfileInstances Id="d0a13992-851d-423d-916d-e1976a34f3f6">
        <profileInstance Id="c7f4eecf-37de-4fb2-b7fa-14747958e8cd" name="StandardProfileL2">
          <elementDefinition Id="e34d544e-0fea-4ed6-ac5e-1b74119ac791" />
        </profileInstance>
        <elementDefinition Id="0caec977-1f8c-4ba3-a7db-8cc9ad9cc73b" />
      </packageHasProfileInstances>
      <packageHasProfileInstances Id="3b4e12c7-adee-45b2-a675-bca964c11f99">
        <profileInstance Id="c9e7d01a-6aa0-4697-8877-cdafc408d6ca" name="StandardProfileL3">
          <elementDefinition Id="532ea607-fb19-44b8-8502-3351b05452be" />
        </profileInstance>
        <elementDefinition Id="29349502-908c-4fda-9054-c48619c59ed0" />
      </packageHasProfileInstances>
    </profileInstances>
  </package>
</logicalClassDesignerModel>

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?

<?xml version="1.0" encoding="utf-8"?>
<logicalClassDesignerModel xmlns:dm0="http://schemas.microsoft.com/VisualStudio/2008/DslTools/Core" xmlns:dm1="http://schemas.microsoft.com/dsltools/Kernel" xmlns:dm2="http://schemas.microsoft.com/dsltools/Component" xmlns:dm3="http://schemas.microsoft.com/dsltools/Activity" xmlns:dm4="http://schemas.microsoft.com/dsltools/Interaction" xmlns:dm5="http://schemas.microsoft.com/dsltools/UseCase" xmlns:dm6="http://schemas.microsoft.com/dsltools/UmlModelLibrary" xmlns:dm7="http://schemas.microsoft.com/dsltools/UmlDiagrams" xmlns:dm8="http://schemas.microsoft.com/dsltools/ModelStore" dslVersion="1.0.0.0" Id="0b035e1e-cf78-4d7b-a9c5-7ae15e246642" name="UMLClassDiagram1" linkedPackageId="30fb2a44-daa7-48cf-a306-c91b92f1dd7b" xmlns="http://schemas.microsoft.com/dsltools/LogicalClassDesigner">
  <packagedElements>
    <logicalClassDesignerModelHasTypes>
      <class Id="5d701b74-ed36-4baa-8318-0406964414ce" name="Class1" isAbstract="false" isLeaf="false" isActiveClass="false">
        <elementDefinition Id="82f92ae7-5d9b-4e64-8497-39c686ce2e5d" />
        <ownedAttributesInternal>
          <property Id="06827ace-592e-4cf7-bc13-946f073f2b73" name="Attribute2" isLeaf="false" isStatic="false" isReadOnly="false" isDerived="false" isDerivedUnion="false" aggregation="None" isComposite="false">
            <elementDefinition Id="3ebbef23-6573-42bf-8542-a3504f1ef28a" />
          </property>
        </ownedAttributesInternal>
        <ownedOperationsInternal>
          <operation Id="c061b4ea-9880-4674-8775-a7b360a2791d" name="Operation1" isLeaf="false" isStatic="false" isAbstract="false" concurrency="Sequential" isQuery="false">
            <elementDefinition Id="fe154244-f792-415d-8ebc-50d5e3a13362" />
            <ownedParameters>
              <operationHasOwnedParameters>
                <parameter Id="6cbf9775-608c-4a1c-bd17-14397c5ce0f3" direction="Return">
                  <elementDefinition Id="f9eabe6e-f42c-4ece-a0fc-6da2a5da11f0" />
                  <type_NamedElement>
                    <referencedTypeMoniker Id="7acb8bf9-6778-42b4-949c-5fbb1ed0a9bc" LastKnownName="String" />
                  </type_NamedElement>
                </parameter>
              </operationHasOwnedParameters>
            </ownedParameters>
          </operation>
        </ownedOperationsInternal>
      </class>
    </logicalClassDesignerModelHasTypes>
    <packageHasNamedElement>
      <referencedType Id="7acb8bf9-6778-42b4-949c-5fbb1ed0a9bc" name="String" isAbstract="false" isLeaf="false" cachedFullName="String">
        <elementDefinition Id="59259974-6d55-42c6-b7bd-763d77ac8ef9" />
      </referencedType>
    </packageHasNamedElement>
  </packagedElements>
  <package Id="30fb2a44-daa7-48cf-a306-c91b92f1dd7b" name="ModelingProject1">
    <elementDefinition Id="7c93d198-9011-4ef0-b6ba-9bd438a05ad5" />
    <profileInstances>
      <packageHasProfileInstances Id="d0a13992-851d-423d-916d-e1976a34f3f6">
        <profileInstance Id="c7f4eecf-37de-4fb2-b7fa-14747958e8cd" name="StandardProfileL2">
          <elementDefinition Id="e34d544e-0fea-4ed6-ac5e-1b74119ac791" />
        </profileInstance>
        <elementDefinition Id="0caec977-1f8c-4ba3-a7db-8cc9ad9cc73b" />
      </packageHasProfileInstances>
      <packageHasProfileInstances Id="3b4e12c7-adee-45b2-a675-bca964c11f99">
        <profileInstance Id="c9e7d01a-6aa0-4697-8877-cdafc408d6ca" name="StandardProfileL3">
          <elementDefinition Id="532ea607-fb19-44b8-8502-3351b05452be" />
        </profileInstance>
        <elementDefinition Id="29349502-908c-4fda-9054-c48619c59ed0" />
      </packageHasProfileInstances>
    </profileInstances>
  </package>
</logicalClassDesignerModel>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文