Trying to better understand Shared Projects and using them in different solutions - c#

Even though Shared Projects have been around since Visual Studio 2015 (maybe as early as VS 2013 update2), I've only recently learned about them. Today I spent time trying to learn how to use them following a tutorial I found Shared Project: An Impressive Feature of Visual Studio 2015 Preview. However, the one thing the author did in that tutorial, which won't work for us, is he created the Shared Project and 3 other projects, all within the same solution. Of course, you can do that, but in practice we're likely to want to create a Shared Project in some solution, and then as time goes by, include that Shared Project in other solutions.
So what I did is instead of putting the Windows Forms application into the same solution as the author of that C# Corner post did, I created a new solution with a Windows Forms project in it, then I tried to add the Shared Project from the first solution. First, I tried adding the .sln file. That failed miserably. Then I tried adding the .shproj file to the second solution. That failed miserably as well.
Next I shared here on SO for ways of addressing this. I found 2 posts: Adding references in a shared (.shproj) project and How do I add a reference to a Shared Code project (.shproj) from another project. The second one gave me an idea. I decided I would simply add the Shared Project, from the first solution, to the second solution by clicking on the second solution within Solution Explorer, then doing a "Add Existing Project". That worked.
But I wonder, is that the way you're supposed to use Shared Projects? If so, it seems to me as though I could just as well created a simple class library in the first solution and then added that class library project to the second solution. Is there something about Shared Projects that make them inherently better to use, if you add the Shared Project to a different solution, instead of just adding a regular class library project to a solution?

A class library compiles into its own DLL and your original project references that DLL, whereas a project using a Shared Project will compile into a single assembly. One scenario I could think of with shared projects is that you can have single code base but has platform specific code sections marked by directives.

There is a good video on this subject even though it's being explained in the context of xamarin they do a good job i think.
https://www.youtube.com/watch?v=G5ov0gLZWgQ
Personally I I would always go with PCL (portable class lib) rather than SAP (shared project). I use shared code projects as documentation container in my projects. The project green icon stands out really well. I keep everything there from markeddown doc files to stored procedures and etc.

Related

Self-built C# Library not found

I have an issue including a self-built library to a C#-project. I have created an own class library called ClassLibrary1 just to learn how to add libraries on Visual Studio 2019.
So I have written some simple code in a newly created .NET-class library project and have clicked on "create new solution" (directly translated from my german IDE-language. Maybe it's called slightly different) after writing the code. Back in the C#-project, I have selected the dll-file from bin/Debug/ of the class library's project folder.
After I have set the checkmark, the dll-file was shown in the solution-explorer under Assemblys like expected. But the issue I now have is that I still cannot use the ClassLibrary1.dll-file in the cs-file in this very project as I expected via the command "using ClassLibrary1;". It only shows me the error message "type- or namespacename "ClassLibrary1" not found" when trying to compile the C#-project and I don't get, why this is the case.
It seems like it has to be a very obvious problem but after some research on the internet and trying some things by myself still nothing has changed.
Thanks in advance for helpful replies.
The by far easiest way to manage a library is to use project references. Ensure that your library and the project that uses the library is in the same solution. Then right click the "references" and select "add Reference", go to the project tab and add a checkbox for the library. Read more about managing references.
You might also need to add namespaces for the classes you wish to use in the source files.
I would not recommend managing using file-references to lose dll-files, since it can easily become a hassle to manage. I.e. if you create a new version of the library you would need to build, and explicitly replace this file in all other projects and update all the references.
If you want to share libraries between multiple solutions the more popular solution would be to setup a nuget server. This solves some of the updating problems by maintaining multiple versions of the same library, and provides a nice interface to update references in all projects. But this is a somewhat more complicated solution, so I would not recommend this for new developers.

Dual project solution, one is dll- embed code into main project?

I writing and open-source winforms app, and my program depends on another open-source project called ObjectListView.
ObjectListView is its own project in my solution and referenced as such, but its output is set as a .DLL.. It's just an enhanced ListView control. Is there any way that the project ObjectListView can be embedded in my own assembly without having to ILMerge or use some form of packer? (Defeating the purpose of an open-source project)
My initial thought was just to manually drop all the source files and whatnot into my own project, but that seems rather clunky and far from an idea solution.
PS; I did try searching for this but I didn't come across much that wasn't related to embedding DLLs into an output assembly.. Not merging projects like this. I apologize in advance if this question has been answered before.
If you don't want a separate DLL, and you don't want an embedded DLL, then perhaps a "Shared Project" project type is what you're looking for. The docs state:
A Shared Project does not get compiled on its own, it exists purely as a grouping of source code files that can be included in other projects. When referenced by another project, the code is effectively compiled as part of that project. Shared Projects cannot reference any other project type (including other Shared Projects).
While I don't think your project is exactly what it's intended for, I think it will work in your scenario.
See What is the difference between a Shared Project and a Class Library in Visual Studio 2015? for more information.

C# Project with either ProjectReference or Reference

we are working on many different applications where each application has its own VS solution (and of course a CS project). Additionally there are many libraries (also CS projects) that are shared between the applications and are included in the VS solution so that you can debug during startup phase of the development.
We also have a "master" solution that contains all projects in one big solution for refactoring issues. Because VS does not support "solutions in solutions" we have to maintain also this master solution. We are talking about 300 assemblies.
Now a point is reached where it's helpful anymore to have all CS projects as ProjectReference in the application solution anymore. For example we have a library that does the CUDA support which can be compiled once and then used as a regular Reference.
The only problem here is that there are occasions where you need the ProjectReference back again when you need to debug into a problem that can only be verified when it runs in conjunction with the main application and cannot be verified in a normal test case.
One option would be to maintain two projects, one for debugging and one for working. The only problem is that once you debug and you need to use the ProjectReference the dependencies cascade down the whole project tree.
For me it feels like that I need two CS projects for each project. One that only contains References and another one that only contains ProjectReferences. Mixing could be a problem. The question is, if there is something automatic where a Referency-only project can be created from a ProjectReference-only project?
How do you solve something like this?

Automatic References for new projects in VisualStudio

I have a Visual Studio solution, for which I'm trying to keep the code fairly well separated, so I'm using quite a lot of projects.
I've got a handful of projects that use all the other projects (Unit-tests, a Bench-marker, a "Main" project). And a couple of the existing projects are going to be used by all the projects (core dependencies, utility projects, etc.)
The upshot is that each time I create a project, I have to give each project in the first group a reference pointing to that new project, and I have give the new project a reference pointing to each project in the second group.
I also have to add any common nuget packages I want (e.g. MoreLinq)
Is there any way to tell VS about these things and get it to do them automatically when I add a new project?
(Running VS2013 if it's relevant)
================================================
Note: It's entirely possible that the correct answer is "you're using too many projects". If you think so, thank you, but please don't feel the need to comment - that's not the question I'm interested in answering at the moment.
You could create a project template new projects in your solution.
See http://msdn.microsoft.com/en-us/library/xkh1wxd8(v=vs.120).aspx for more details.

Create a single "dll" starting from two independent projects (same solution)

I have a small solution with 2 projects "Class Library" independent, ie, no project is used as a reference in another.
The first project is called "Extension1" and the second "extension2". I would not want to use this solution in another project and having to reference the two dlls separately, I need to create a single dll "CustomExtension.dll" and that it has the two ("Extension1.dll" and "Extension2.dll") to reference only dll "CustomExtension.dll" on a new project using only the respective namespace. Is this possible?
PS: Windows 8 - Visual Studio 2010 Professional - C#
Might want to take a look at ILMerge. We use it in a lot of our projects for combining multiple dll's into one.
http://research.microsoft.com/en-us/people/mbarnett/ilmerge.aspx
http://www.microsoft.com/en-us/download/details.aspx?id=17630
It seems like this could be a very tedious task. Take a look at this answer to a previous question:
https://stackoverflow.com/a/6573711/329928
You have to do that by compiling the code using csc.exe outside visual
studio and passing the command-line parameters yourself (all .cs
files).
This will be a bit tedious but can be done. Look here: http://msdn.microsoft.com/en-us/library/78f4aasd.aspx

Categories