简体   繁体   English

从多个项目中引用C#代码

[英]Make reference to C# code from multiple projects

I have a .cs file full of C# code I keep reusing in multiple projects. 我有一个.cs文件,里面装满了C#代码,我一直在多个项目中重复使用。

Right now I'm including it in these projects by copying and pasting the code into a new file in each project directory. 现在我通过将代码复制并粘贴到每个项目目录中的新文件中来将它包含在这些项目中。 This is the wrong way to do it. 这是错误的做法。

What's the right way to do it? 什么是正确的方法呢?

The right way should: 正确的方法应该:

  • keep the common code in only one place on my computer 将公共代码仅保留在计算机上的一个位置
  • and keep the link fresh --- so when the common code changes, each project is aware of it, the next time I recompile that project. 并保持链接新鲜 - 所以当公共代码更改时,每个项目都知道它,下次我重新编译该项目。

Randomly guessing, I'd expect some sort of directive like using mycode = C:\\common\\mycode.cs , but I'm sure that's not the .NET way of doing things. 随机猜测,我希望某种指令如using mycode = C:\\common\\mycode.cs ,但我确信这不是.NET的做事方式。

(I'm using C# 2010, .NET 4.0, and only compiling this code locally on one computer.) (我正在使用C#2010,.NET 4.0,并且只在一台计算机上本地编译此代码。)

将代码放入单独的类库项目中,并在其他项目中引用该项目。

Or, 要么,

  • Right-Click on the project in Solution explorer 右键单击Solution explorer中的项目
  • Select Add Existing Item 选择添加现有项目
  • Browse to the .cs file in another project, and Single-click to select the file 浏览到另一个项目中的.cs文件,然后单击以选择该文件
  • Click the down-arrow button to the right of Add and select Add as Link 单击“ 添加 ”右侧的向下箭头按钮,然后选择“ 添加为链接”

You now have one source file referenced by two projects but it has the namespace you gave it in the first project which might not be ideal. 您现在有两个项目引用的一个源文件,但它具有您在第一个项目中提供的名称空间,这可能并不理想。

The best way of organizing that is to as the two other answers have suggested, put common code in a class library so it has a namespace of MyClassLibrary rather than SomeOtherProject. 最好的组织方式是,正如其他两个答案所建议的那样,将公共代码放在类库中,使其具有MyClassLibrary的名称空间而不是SomeOtherProject。 It saves a larger dll being copied which doesn't matter much until you come to develop for something small like Windows Phone. 它可以节省更大的dll被复制,这对你开发像Windows Phone这样的小东西来说并不重要。 Or change the namespace of common code to be Me.Common in all your apps - it doesn't really matter which one is the original, you can edit it from any project that references it. 或者在所有应用程序中将公共代码的命名空间更改为Me.Common - 哪个是原始的并不重要,您可以从引用它的任何项目编辑它。

Check that Source Control isn't a problem. 检查源控制不是问题。

Create a Class Library, add the file, build the project, and reference the DLL created from the build. 创建类库,添加文件,构建项目,并引用从构建创建的DLL。 Add the using statement to each file that will reference it. 将using语句添加到将引用它的每个文件。 Also if it errors and the DLL is in the Project you and Right Click on the object -> Resolve and it will add the using for you. 此外,如果它出错,并且DLL在项目中,您和右键单击对象 - >解析,它将为您添加使用。

I wrote an app that automates adding a code as a link, handy for projects that reuse a lot of code and change a bit. 我编写了一个应用程序,可以自动添加代码作为链接,方便重用大量代码和更改的项目。 It's at https://github.com/CADbloke/CodeLinker 它位于https://github.com/CADbloke/CodeLinker

or 要么

Don't be afraid to edit XML .csproj files. 不要害怕编辑XML .cs​​proj文件。 For instance, this works ... 例如,这有效......

<Compile Include="$(Codez)\z.Libraries\diff-match-patch\DiffMatchPatch\**\*.cs"
Exclude="NotThisOne.cs;**\NotThisFolderWith\This*.cs">
<Link>Libs\%(RecursiveDir)%(Filename)%(Extension)</Link>
</Compile>

...and will give you all the C# files from the source folder, and subfolders, as linked files in your destination project. ...并将从源文件夹和子文件夹中将所有C#文件作为目标项目中的链接文件提供给您。

  • $(Codez) is a Windows Environment Variable I use on my PCs. $(Codez)是我在PC上使用的Windows环境变量。
  • I also could have used *.* at the end instead of *.cs . 我也可以在结尾使用*.*而不是*.cs
  • This is one of those things Visual Studio might break on you, adding a file into the folder full of wildcard-linked files may break them out to separate entries. 这是Visual Studio可能会破坏您的一件事,将文件添加到充满通配符链接文件的文件夹中可能会将它们分解为单独的条目。 Or not. 或不。 Depends on the wind. 取决于风。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM