如何编译C#代码作为库而不是可执行文件?
我在Visual Studio 2010中有一个C#控制台应用程序。它有一个Main()方法以及一堆实用程序类。我希望这些实用程序类可用于其他解决方案。从在线阅读看来,我需要将其编译为类库(DLL)。所以这就是我所做的:
I have a C# console application in Visual Studio 2010. It has a Main() method as well as a bunch of utility classes. I'd like those utility classes to be available to other solutions. From reading online it seems that I need to compile it as a Class Library (DLL). So here's what I did:
- 在Visual Studio中将Project> [ProjectName] Properties> Application改为Output type 控制台应用程序到类库
- 重建; ProjectName.dll是在bin / Debug中创建的。
- 创建了一个新的控制台应用程序
- 解决方案资源管理器>添加参考>浏览到ProjectName.DLL,选择
- Went in Visual Studio to "Project > [ProjectName] Properties > Application" and changed "Output type" from "Console Application" to "Class Library"
- Rebuilt; ProjectName.dll was created in bin/Debug.
- Created a new Console Application
- Solution Explorer > Add Reference > browse to ProjectName.DLL, select it.
但是,IntelliSense和对象浏览器都不能在该DLL内找到类。
However, neither IntelliSense nor the Object Browser could find the classes inside that DLL.
我尝试将几个不同的控制台应用程序重新编译为类库,并获得相同的结果。我也注意到,如果我最初创建解决方案作为类库,但不是如果我将它转换为一个以后,它是有效的。
I tried recompiling several different Console Applications as Class Libraries and got the same result. I also noticed that it works if I initially create the solution as a Class Library, but not if I convert it to one later.
任何提示?
您不需要将其构建为dll。 VS 2010(和IIRC 2008)允许引用exe程序集。所有你需要的是他们的相关类型被宣布 public
- 顶级类defualt到内部
如果你不t添加一个说明符。
You do not need to build it as a dll. VS 2010 (and IIRC 2008) allow referencing exe assemblies. All you need is for they relevant types to be declared public
- top-level classes defualt to internal
if you don't add a specifier.