引用多个项目中的C#代码 [英] Make reference to C# code from multiple projects

查看:52
本文介绍了引用多个项目中的C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .cs 文件,里面充满了C#代码,我一直在多个项目中重复使用.

I have a .cs file full of C# code I keep reusing in multiple projects.

现在,我通过将代码复制并粘贴到每个项目目录中的新文件中,将其包括在这些项目中.这是 错误 的处理方式.

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?

正确的方法应该:

  • 将通用代码仅保留在计算机上的一个位置
  • 并保持链接的最新状态---因此,当通用代码更改时,每个项目都将在下一次我重新编译该项目时就意识到这一点.

随机猜测,我期望某种指令,例如使用mycode = C:\ common \ mycode.cs ,但我确定这不是.NET的处理方式.

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.

(我正在使用C#2010,.NET 4.0,并且仅在一台计算机上本地编译此代码.)

(I'm using C# 2010, .NET 4.0, and only compiling this code locally on one computer.)

推荐答案

创建一个类库,添加文件,构建项目,并引用从构建中创建的DLL.将using语句添加到将引用它的每个文件中.同样,如果它出错并且DLL在项目中,则您可以右键单击对象->解决,它将为您添加using.

Create a Class Library, add the file, build the project, and reference the DLL created from the build. Add the using statement to each file that will reference it. 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.

这篇关于引用多个项目中的C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆