我有两个有一些公共代码的解决方案,所以我想把它提取出来并在它们之间共享。此外,我希望能够独立地发布这个库,因为它可能对其他人有用。

用Visual Studio 2008最好的方法是什么? 一个项目是否存在于多个解决方案中? 对于这段单独的代码,我有单独的解决方案吗? 一个解决方案能依赖于另一个解决方案吗?


当前回答

涉及的两个主要步骤是

1-创建c++ dll

在visual studio

New->Project->Class Library in c++ template. Name of project here is first_dll in 
visual studio 2010. Now declare your function as public in first_dll.h file and 
write the code in first_dll.cpp file as shown below.

文件代码

// first_dll.h

using namespace System;

namespace first_dll 
{

public ref class Class1
{
public:
    static double sum(int ,int );
    // TODO: Add your methods for this class here.
};
}

Cpp文件

//first_dll.cpp
#include "stdafx.h"

#include "first_dll.h"

namespace first_dll
{

    double Class1:: sum(int x,int y)
    {
        return x+y;
    }

 }

检查这个

**Project-> Properties -> Configuration/General -> Configuration Type** 

这个选项应该是Dynamic Library(.dll),现在就构建解决方案/项目。

在Debug文件夹中创建first_dll.dll文件

2-在c#项目中链接它

开放c#项目

Rightclick on project name in solution explorer -> Add -> References -> Browse to path 
where first_dll.dll is created and add the file.

在c#项目的顶部添加这一行

Using first_dll; 

现在可以在某些函数中使用下面的语句访问dll中的函数

double var = Class1.sum(4,5);

我在VS2010的c++项目中创建了dll,并在VS2013的c#项目中使用。它工作得很好。

其他回答

现在您可以使用共享项目了

Shared Project is a great way of sharing common code across multiple application We already have experienced with the Shared Project type in Visual Studio 2013 as part of Windows 8.1 Universal App Development, But with Visual Studio 2015, it is a Standalone New Project Template; and we can use it with other types of app like Console, Desktop, Phone, Store App etc.. This types of project is extremely helpful when we want to share a common code, logic as well as components across multiple applications with in single platform. This also allows accessing the platform-specific API ’s, assets etc.

更多信息请看这个

创建一个包含所有常用功能的dll类库是个好主意。无论其他解决方案如何,每个解决方案都可以独立地引用此dll。

事实上,在我的工作中,这就是我们资源的组织方式(我相信在许多其他地方也是如此)。

顺便说一下,解决方案不能显式地依赖于另一个解决方案。

您可以在多个解决方案中包含相同的项目,但是您一定会在某个时候遇到问题(例如,当您移动目录时,相对路径可能会失效)。

经过多年的努力,我终于提出了一个可行的解决方案,但它要求您使用Subversion进行源代码控制(这并不是一件坏事)

在解决方案的目录级别,添加一个svn:externals属性,指向您希望包含在解决方案中的项目。Subversion将从存储库中提取项目,并将其存储在解决方案文件的子文件夹中。解决方案文件可以简单地使用相对路径来引用项目。

如果我有更多的时间,我会详细解释这一点。

在另一个项目中包含一个项目的类文件的一个更简单的方法是在现有解决方案中添加该项目,然后在现有项目中添加新项目的DLL引用。最后,您可以通过在任意类的顶部声明using指令来使用添加的类的方法。

您可以使用以下技术(这是@Andomar的解决方案保存在.csproj中的方法)内联通配符

<Compile Include="..\MySisterProject\**\*.cs">
  <Link>_Inlined\MySisterProject\%(RecursiveDir)%(Filename)%(Extension)</Link>
</Compile>

投入:

    <Visible>false</Visible>

如果你想隐藏文件和/或防止通配符包括被扩展,如果你添加或删除一个项目从“虚拟现有项目”文件夹,如上面的MySisterProject。