Use existing source code in another project / namespace

I have two library projects in one solution (in addition to other projects), which must necessarily separate certain classes, but must remain separate for reasons of automatic updating.

For shared classes, I would ideally want to use the same class.cs file in both libraries, so I don’t need to constantly check that changes to the class are propagated through both libraries.

However, the namespaces of the two libraries are different, so a different declaration of namespace classlib {} is required for the file containing the class in each library.

I am using a git repo if there is a way to do this using branch / merge operations.
Currently 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 } } 
+5
source share
3 answers

Declare a SharedClass in a common namespace instead of two different namespaces. You can associate a file with projects, and not include it physically.

From MSDN :

You are referencing a file from your project in Visual Studio. In Solution Explorer, right-click your project and select Add Existing Item. Or 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 different way of processing is to use the T4 template with some logic to dynamically create a file. The contents of the * .tt template files (not * .cs files!):

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

And in another library

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

The class file itself will not declare a namespace.

+4
source

Based on the information you provided, if your common classes are truly β€œdistributed”, you should create a third library that both of your main libraries can reference. eg:

MainLib1 ( link commonLib )

MainLib2 ( link commonLib )

commonLib (includes class.cs and other common code)

+3
source

I need to be able to update library.dll files separately

Then you should use submodules for this task.

A submodule is different git repositories under the same root.
This way you can manage 2 different projects at the folder level inside the root repository

enter image description here

+3
source

Source: https://habr.com/ru/post/1238887/


All Articles