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

淺唱寂寞╮ 提交于 2019-12-11 03:45:00

问题


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?


回答1:


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

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.




回答2:


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




回答3:


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.


来源:https://stackoverflow.com/questions/28171416/visual-studio-2013-c-one-solution-consume-code-of-another-solution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!