有没有一种方法可以使用 Resharper 通过一个模板/宏生成多个文件?

发布于 2024-12-26 12:08:33 字数 353 浏览 3 评论 0原文

当我为应用程序编写 API 时,我总是遵循一个设定模式(谁不这样做!?),如下所示:

User (Folder/Namespace)
    User.cs
    UserDao.cs
    UserDaoFactory.cs
    UserService.cs
    UserServiceFactory.cs

我知道 Resharper 有我已经使用的文件模板,但如果我不必单独添加每个模板,那就太好了当您在应用程序中处理超过 30 个实体时,该文件可能会变得非常乏味。如果您无法使用 resharper 执行此操作,是否可以使用 Visual Studio 制作某种宏来执行您的 resharper 文件模板?

When I write my APIs for applications I always follow a set pattern (who doesnt!?) that looks like this:

User (Folder/Namespace)
    User.cs
    UserDao.cs
    UserDaoFactory.cs
    UserService.cs
    UserServiceFactory.cs

I know Resharper has file templates which I already utilize, but it would be great if I didn't have to individually add each file which can get extremely tedious when you are working with upwards of 30 entities in an app. If you can't do this with resharper is there anyway to make some kind of macro with Visual Studio that will execute your resharper file templates?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

安静被遗忘 2025-01-02 12:08:33
  1. 在单个文件中创建所有这些类(如果需要,请创建一个包含标准类集的文件模板)
  2. 编写逻辑
  3. Shift+Alt+L 将焦点放在解决方案资源管理器中的文件上
  4. Ctrl+Shift+R 显示重构此菜单以及适用重构的列表。
  5. 选择将类型移至匹配文件,完成重构向导,并观察类分成单独的文件。
  6. 要将所有新创建的类放入一个文件夹中,请在解决方案资源管理器中选择它们,然后再次调用重构
  7. 这次,选择移至文件夹
  8. 在重构向导中指定新文件夹的名称,按创建此文件夹,然后完成向导。
  1. Create all these classes in a single file (create a file template containing the set of standard classes if you want to)
  2. Write your logic
  3. Press Shift+Alt+L to put focus on the file in Solution Explorer
  4. Press Ctrl+Shift+R to display Refactor this menu with a list of applicable refactorings.
  5. Select Move Types into Matching Files, complete the refactoring wizard, and watch the classes separating into individual files.
  6. To put all the newly created classes into a folder, select them in Solution Explorer, and again invoke Refactor this.
  7. This time, select Move to Folder.
  8. Specify the name of the new folder in the refactoring wizard, press Create this folder, and complete the wizard.
你的往事 2025-01-02 12:08:33

看起来 R# 8 添加了此功能

来自 Resharper 帮助页面

从ReSharper 8.0开始,每个文件模板可以添加多个
应用时的文件。这>可能对 WPF、Web 和其他有帮助
相关数据可以保存在不同文件中的项目。

以下是您需要了解的有关多文件模板的一些事项:

  • 文件模板始终包含一个主文件,并且可以选择包含任何
    辅助文件的数量。因此,一旦您添加辅助文件到
    任何文件模板,您都会获得多文件模板。
  • 添加时
    辅助文件,您可以引用任何现有的文件模板(添加文件
    来自现有模板选项)。请注意,如果引用的模板
    也有多个文件,那么只有引用的主文件
    应用模板时将添加模板。
  • 一套
    参数跨越多文件模板的所有文件。即,如果你使用
    多文件模板的多个文件中的相同参数 $PARAM$,
    当模板被使用时,它在所有相关文件中将具有相同的值
    应用。
  • 如果多文件模板具有可编辑参数,则 Hot
    当您应用此模板时部署的 Spot 会话将指导您
    您浏览所有创建的需要用户输入的文件。

Looks like R# 8 added this feature

From the Resharper Help Page

Starting from ReSharper 8.0, each file template can add more than one
file when it is applied. This > may be helpful for WPF, Web and other
projects where related data can be saved in different files.

Here are a few things you need to know about multi-file templates:

  • A file template always include one main file, and, optionally, any
    number of secondary files. So as soon as you add a secondary file to
    any file template you get a multi-file template.
  • When adding a
    secondary file, you can reference any existing file template (Add file
    from existing template option). Note that if the referenced template
    has multiple files too, then only the main file of the referenced
    template will be added when you apply the template.
  • The set of
    parameters spans all files of a multi-file template. I.e., if you use
    the same parameter $PARAM$ in several files of a multi-file template,
    it will have the same value in all related files when the template is
    applied.
  • If a multi-file templates has editable parameters, the Hot
    Spot Session, which deploys when you apply this template, will guide
    you through all created files where the user input is required.
深者入戏 2025-01-02 12:08:33

我会考虑将 .tt 模板与 ReSharper 一起使用。

  1. 使用 ReSharper 创建主文件。
  2. 使用一个或多个tt文件自动生成其中包含partial关键字的依赖(.partial.cs)文件。
  3. 如果需要,扩展生成的类。

I would consider using .tt templates with ReSharper.

  1. create master file with ReSharper.
  2. using one or many tt files to auto generate dependent (.partial.cs) files with partial keyword in it.
  3. extend generated classes if needed.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文