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
}
}
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.
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.
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.)
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With