简体   繁体   English

在没有 Visual Studio 的情况下跨项目共享类

[英]Sharing classes across projects without Visual Studio

I know this is by no means an unexplored issue, but my problem is pretty simple.我知道这绝不是一个未经探索的问题,但我的问题很简单。 I have an MVC application with all of my models, and I'm just finishing up a document generator for it.我有一个包含所有模型的 MVC 应用程序,而我刚刚为它完成了一个文档生成器。 Ideally I would like to painlessly just inject it into this application after packaging the document generator separately, though I'm not sure if this is the best way of going about this.理想情况下,我想在单独打包文档生成器后轻松地将其注入此应用程序,尽管我不确定这是否是解决此问题的最佳方式。

The main issue is for my document generator I really need to access many models such as Discrepancy, WorkOrder, Part, etc. so they can be passed as arguments.主要问题是对于我的文档生成器,我确实需要访问许多模型,例如差异、工作订单、零件等,因此它们可以作为 arguments 传递。 What is the easiest, most loosely coupled way of sharing these classes across both my MVC application and my document generator?在我的 MVC 应用程序和文档生成器之间共享这些类的最简单、最松耦合的方式是什么? I'm using .NET Core and Visual Studio Code, so I don't have Visual Studio's UI to help add links or shared projects.我正在使用 .NET Core 和 Visual Studio Code,所以我没有 Visual Studio 的 UI 来帮助添加链接或共享项目。

I don't know the ideal way to share a class across two independent projects here我不知道在这里的两个独立项目之间共享 class 的理想方式

Ideal way is to create new project with classes you want to share and reference it from both projects.理想的方法是使用要共享的类创建新项目,并从两个项目中引用它。

In the root directory of your solution在解决方案的根目录中

dotnet new classlib -n MyModels -t netstandard2.0

Move model classes into new project将 model 类移动到新项目中
Then in document generator directory reference just created models project然后在文档生成器目录中引用刚刚创建的模型项目

dotnet add reference ../MyModels/MyModels.csproj

Done完毕

You can create a solution file to represent your projects您可以创建一个解决方案文件来代表您的项目

dotnet sln MySolution.sln add MyModels/MyModels.csproj Documents/Documents.csproj

Pretty sure there are some extensions for Visual Studio Code to handle c# projects and solutions很确定有一些 Visual Studio Code 扩展来处理 c# 项目和解决方案

For example: vscode-solution-explorer例如: vscode-solution-explorer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio项目的.NET类? - .NET classes for Visual Studio projects? 在项目之间共享C#中的代码而不公开类 - Sharing code in C# between projects without making classes public 如何在没有Visual Studio的情况下管理大型项目? - How to manage large projects without Visual Studio? 如何在不打开Visual Studio的情况下绑定项目 - how to bind projects without opening visual studio 如何在Visual Studio 2012中跨多个项目共享CSS文件? - How to share CSS file across multiple projects in Visual Studio 2012? 在1个解决方案中的多个项目之间共享资源。 Visual Studio 2010 - Share Resources Across Multiple Projects In 1 Solution. Visual Studio 2010 Visual Studio:跨项目引用资源的正确方法是什么? - Visual Studio: What's the correct way to reference resources across projects? 跨解决方案与.Net Core,WPF和Xamarin的多个项目共享图像和类 - Sharing images and classes across Solution with Multiple Projects for .Net Core, WPF and Xamarin 2个项目1个解决方案共享课程? - 2 projects 1 solution- sharing classes? Visual Studio 2008-两个项目可以共享公共类吗? - Visual Studio 2008 - Is it possible for two projects to share common classes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM