Visual Studio 2013 C#: one solution consume code of another solution
Asked Answered
D

3

2

I have Java background and am trying to start on C#.

I wanna create a lib in C# that will be used in other solutions. In Eclipse it's just a matter of creating a jar and adding it to classpath. I know each project in VS2013 becomes a dll, but how can I make a solution see these dll?

Also, in Eclipse, we can create a Web Fragment Project. It can have Servlets, jsp and static js and css files, it becomes a war file and can be imported into another project and its files be used in that project.

How can I do that in VS2013? I'd like to create a solution with static files, master page, some aspx stuff, C# dll, and then use them all in other solutions.

Is there any tutorial (I googled it but found nothing) teaching how to do it?

Duhl answered 27/1, 2015 at 13:14 Comment(0)
E
4

You have a few options depending on your preferences and scope

Option 1 - The Class Library

You can create Class Library, that can be referenced in your website project. The Class library is a library of classes, interfaces, and value types

You can either Add an existing/New Class Library project to your website solution and reference it directly

  1. You can add the project to your solution by right clicking the
    solution (inside VS) -> Add -> Existing project -> and navigating to said
    project's .csproj file

or

You can use a new/existing Class Library Project - build it and reference the built dll in your website solution.

  1. you can right click your website solution (inside VS) -> Add -> new project -> choose Class Library

After you've done one of the above ->

  1. Right click the project, you want to add the reference to
  2. Click "Add Reference"
  3. navigate to the .dll in question.

If the dll you want to reference is part of your current solution (as in step 1) -> after you've pressed "Add Reference" - press the "Solution" Tab and it should show up

enter image description here

enter image description here After you've added the dll. Remember to reference it in your code files with

Using TheReferenceNamespace;

which will allow you to call the functions inside you dll like the following

FunctionInsideDll(param);

or you could fully qualify your calls instead, like the following

TheReferenceNamespace.FunctionInsideDll(param); 

Option 2 - Share MasterPages

if you just want "shareable" masterpages you can do the following - (taken from this -> MSDN article) (for future reference - web archive link - just in case something gets moved)


Precompile the Code Used in a Master Page

If you are concerned about code in your master pages being visible to others reusing the pages, you can precompile the master pages' code into a library. In this library, you can include code-behind pages as well as user or custom controls. Compiling master pages does not remove the declarative code for the master files or any server controls used, but you can compile the master files to remove the code for controls or code-behind pages used by the master pages.

If you choose to compile the master pages into a library, you must use the "updatable" build option that allows for later modification of the markup. This option is determined by the Allow the precompiled site to be updatable check box in the Publish Web Site dialog box. For more information about precompiling pages into a library that can be reused, see Building Re-Usable ASP.NET User Control and Page Libraries with VS 2005.


Option 3 - The template

Create a template, and use that template for different projects In Visual Studio - Press "File" -> Export Template -> follow the wizard. After it has been exported and you've imported it (either through a checkmark in the wizard or double clicking the vsix file) -it will show up under your project templates when you create a new project. enter image description here enter image description here

Eileen answered 27/1, 2015 at 13:15 Comment(6)
The last bit is not really required, but helps keeping your code neat and tidy.Michaud
@DaveVandenEynde True, you could fully qualify what ever functions you use from the dll :) - thanks for reminding me :)Eileen
Thanks a lot! Pictures aren't appearing here.Duhl
Regarding the second question I did. How can I do the same with a WebSite project? I see I can right click the Solution and go Add > Existing Web Site. But can I add any website project and add a new website project, and local one be able to consume masterpages, pages and static assets from the remote one?Duhl
I don't know about that - I've never come across a project where i'd have masterpages in a separate project - as these tend to be pretty "specific" for that project - unless you mean you want a template that you can use across many projects?Eileen
Yes I mean that, I wanna create a template and use it in other solutions. Thanks a lot for the answer!Duhl
S
1

You can include a project from solution A in solution B by right-clicking on solution B and choosing "Add existing project"

Stereometry answered 27/1, 2015 at 13:19 Comment(0)
D
0

Don't be afraid to edit XML .csproj files. For instance, this works ...

<Compile Include="$(Codez)\z.Libraries\diff-match-patch\DiffMatchPatch\**\*.cs"
Exclude="NotThisOne.cs;**\NotThisFolderWith\This*.cs">
<Link>Libs\%(RecursiveDir)%(Filename)%(Extension)</Link>
</Compile>

...and will give you all the C# files from the source folder, and subfolders, as linked files in your destination project under a folder called \Libs\.

  • $(Codez) is a Windows Environment Variable I use on my PCs.
  • I also could have used *.* at the end instead of *.cs.
  • This is one of those things Visual Studio might break on you, adding a file into the folder full of wildcard-linked files may break them out to separate entries. Or not. Depends on the wind.
Drought answered 28/10, 2015 at 3:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.