Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use existing source code in another project/namespace

I have two library projects within a single solution (in addition to other projects) which necessarily need to share certain classes but must remain separate for automatic-update reasons.

For the classes that are shared, I would ideally like to use the same class.cs file in both libraries so that I don't have to consistently check that changes to the class are propagated through both libraries.

However the namespaces of the two libraries are different, and so the class-containing file in each library requires a different namespace classlib {} declaration.

I am using a git repo, if there is a technique to do this through branch/merge operations.
Presently using VS2013.

How can I achieve this?


Example:

library1.dll

namespace library1
{
    public class SharedClass
    {
        /// code must match SharedClass in libary2
    }
}

library2.dll

namespace libary2
{
    public class SharedClass
    {
        /// code must match SharedClass in library1
    }
}
like image 203
khargoosh Avatar asked Dec 21 '15 21:12

khargoosh


People also ask

How do I use a namespace in another project?

First, you need to add the project which contains the utility operations as a reference to the project which will be using them. If you're using Visual Studio Code or command line, you can use dotnet add reference command to do that, and in Visual Studio you can use Reference Manager.

Can a namespace contain another namespace?

A namespace can contain another namespace. It is called nested namespace. The nested namespace and its members can also be accessed using the dot (.) operator.

How do I add a reference to another project in C?

Add a reference In Solution Explorer, right-click the References or Dependencies node, and then choose either Add Project Reference, Add Shared Project Reference, or Add COM Reference from the context menu. (You can right-click the project node and select Add from the fly-out menu to choose from these options, too.)

How do you change the namespace of a project in Visual Studio?

You can change the default namespace: -> Project -> XXX Properties... Instead of Find/Replace, you can also right click the namespace in code and Refactor->Rename.


2 Answers

Declare the SharedClass in a common namespace, instead of two different namespaces. You could link the file into the projects instead of including it physically.

From msdn:

You link to a file from your project in Visual Studio. In Solution Explorer, right-click your project, and then select Add Existing item Or, you can type Shift+Alt+A. In the Add Existing Item dialog box, select the file you want to add, and in the Add drop-down list, click Add As Link.

namespace Khargoosh.MathLib.Common   { public class SharedClass { ... }  }
namespace Khargoosh.MathLib.Library1 { ... }
namespace Khargoosh.MathLib.Library2 { ... }

or

namespace Khargoosh.MathLib          { public class SharedClass { ... }  }
namespace Khargoosh.MathLib.Library1 { ... }
namespace Khargoosh.MathLib.Library2 { ... }

A completely other way of handling that would be to use the T4 template with some logic to create the file dynamically. Content of the *.tt template files (not *.cs files!):

namespace library1
{
<#@ include file="MyCommonClass.cs"#>
}

And in the other library

namespace library2
{
<#@ include file="MyCommonClass.cs"#>
}

The class file itself would not declare a namespace.

like image 125
Olivier Jacot-Descombes Avatar answered Nov 12 '22 12:11

Olivier Jacot-Descombes


Based on the info you've provided, if your shared classes are truly "common" you should create a 3rd library that both of your main libs can reference. for example:

MainLib1 (reference commonLib)

MainLib2 (reference commonLib)

commonLib (includes class.cs and other common code)

like image 32
spencerwjensen Avatar answered Nov 12 '22 12:11

spencerwjensen