How does a "stand alone" class file work? - c#

I'm looking into the source code of Kestrel and found something interesting. From its dev branch, you can see there is a stand alone class file named HttpClientSlim in /test/shared folder which doesn't belong to any project. And it's used in a few projects. I cloned the source code, the solution explorer looks like
how does it compile?

When you attempt to add an existing item to a project there is a small dropdown next to the "Add" button which allows you to add as a link.
This means that if you want a class included in multiple projects you can keep the actual source file in one place, but "link it in" to multiple projects.
It saves copy/pasteing code, but the project compiles it into the binary as if it were local.
Note that for users of VS2015, you may need to manually edit project.json to get the same functionality, this is described in more detail in this Q/A:
When adding an existing item with "Add as Link", this file is not compiled in VS2015?

Related

Using a Form in a separate Project [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Has anyone successfully been able to use a form created in one project in another with VS2015?
I have created many forms that I usually need to include in most of my projects, however whenever I try to add them to the new project I can never seem to get them to work.
I would like to add them to a separate project, with their code and designer/image references etc, but I have only been successful in adding the form with code no resources or designer.
Instead I am currently having to manually re-create the same form in a new project and copy and paste and reset all the controls and labels and what not in the newer solution.
In the past I used to do it like this guy did: https://www.youtube.com/watch?v=FPBMoibAmU0 But it doesn't seem to work with VS2015?
Surely there's a simple way to do this? My way works but it's very time consuming and I think unnecessary.
So what I used to do in the past: Copy the form1.Designer.cs, form1.cs into the new solutions directory, then in the new solution, right-click the project properties, add existing file and browse and select the two files and click OK.
Yes, people re-use code all the time, this question is coming from a fundamental misunderstanding about how code and projects work. Code intended to be shared should be created in a shared project or shared/distributed library.
Using a distributed library:
Create a class library (.dll)
Add a reference to System.Windows.Forms
Define any code, forms, resources that are intended to be re-used here.
Compile library and retrieve .dll file.
Copy .dll file into your new project and add a reference to it.
Use the form.
This is essentially how the nuget package manager works except it handles managing the physical .dll files and adding of references automatically. If you will be the only consumer of this and you don't intend to check your source code into a source control server (git, tfs, svn, etc) then you could also store this dll only in a single location (your documents folder, etc) so updating it is easier.
If you want to be able to make changes to the original source code in all the projects that use the form, you can instead add the original shared lib project to your new VS solution by right clicking on your solution, clicking add existing project, and navigating to the csproj file of your library project.
Wow I finally worked it out,
To add a existing form of another project to current:
Right-click the Project Properties in Solution Explorer.
Click "Add".
Click "Existing Item".
Browse only for the "Form1.cs" and select it, Click "OK".
Right click the added "Form1.cs" and select "View Code".
Look at top for "namespace", highlight the actual namespace eg: "namespace Form1", so highlight "Form1".
Right-click highlighted namespace and select "Rename...".
A box appears, tick "Include Comments", "Include Strings", then type the new name in(with still the Rename box visible) and Click "OK".
Now just add the resource files to the current project.

How do I compile specific sourcecode files in Visual Studio?

So in VS, I create a project, right? Let's say I'm following a tutorial that has extra code that I don't need that only serves to show examples for looping, threads, etc. - then moves onto the real code. I take notes of everything so I usually save a copy of my source code with a different name as future notes. Now the only thing with projects in VS or any other C# compiler is that if you save the file AS "..." or make a new file, sometimes I find that the compiler will now compile the new code or the old code (it gets me so mixed up).
I get why it does this though, to link to other sourcefiles right? Well anyway, all I want to know is how do I compile a different C# file within my project folder? I don't want to create a new project, it just makes it harder for me to organize.
Assuming you want to include a lot of files into project bu only compile one you can just change "Build action" to "none" from "compile" for CS files you don't want to build.

Visual Studio 2013 only builds HTML changes, not C# changes

I am building a personal website using asp.NET's webforms in visual studio 2013 express for web and am following this tutorial:
http://www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/introduction-and-overview
My page is structured exactly the same as in the website, I have changed some minor stuff to make it my own but the structure in terms of the C# classes and how the interact with the HTML are exactly the same.
I got to section 5 of the tutorial "Display Data Items and Details" and everything was working fine. I've used git a lot in the past so I decided to create a repository for this project so I can access it at work if I feel like.
Suddenly now when I make changes to the C# classes it won't build. It's even stranger because I if I make a change on an HTML file the change is built. In section 3 of the tutorial we learnt how to make the 'product' classes which are displayed on the products page. If I want to change one of the product names for example, when I build the change is not there. Simultaneously I went and changed some info in the HTML for the contact page, IT CHANGES when I build. Why wont the C# changes take effect when I build any more?
I am relatively new to both asp.NET and visual studio. The HTML changes when I build and the C# does not. When I change either I can see in solution explorer that there is a red tick for pending changes. Why would only the HTML pending change be included in the build and not the C#? How do I ensure that the build is actually building the version I see in my editor window?
EDIT
I do not know if I found the original cause but I found a solution/workaround. I realized that the classes mentioned above were grabbed by the html page from the page's database. The .mdf file for the solution was not being rebuilt whenever I cleaned and built so I physically deleted it and rebuilt the solution and voilla my C# changes occurred. I am still fairly new to this whole thing, can someone explain what the .mdf file does and why it wasn't being rebuilt?
Check your .cs files properties on the properties window in visual studio to make sure their Build Action is set to "Compile", Things that are not set to "Compile" do not get compiled. How MSBuild treats project items depends entirely on their build action. CS files default to "Compile" when you make them, but if you changed them yourself that would be why it doesn't update. Also CS files placed in the App_Start folder default to "Content" and they are compiled by ASP.Net when the Application Starts, so if you changed something in App_Start you need to reset the site.
Not exactly sure what is the problem, but I would do a right mouse click on the solution in Solution Explorer->Clean Solution, then do another build and see if that helps.

Shared AssemblyInfo for uniform versioning across the solution

I've read about this technique: Shared assembly info in VS projects - JJameson's blog
Basically it means to create a SharedAssemblyInfo.cs with versioning information about the assembly, and adding this file as Link to all projects of the solution, so the actual file resides only in 1 location on disk.
My question deals with 2 scenarios:
Existing solution that doesn't use this mechanism: Is there a way to easily add the ShareAssemblyInfo to all projects? (lets say i have a solution with 50 projects).
When creating a new project, by default a new AssemblyInfo.cs is created. However i'd like to link automatically to the SharedAssemblyInfo as well.
Is there any solution for this? what is the common practice?
It is possible to link to a shared assembly info file in VS 2010. Ashish Jain has a good blog post about it: Sharing assembly version across projects in a solution.
After creating the shared assembly info file at the solution level, his instructions for linking to it from a project are:
Right click on the project, in which you wish to add the Shared
assembly file, and select Add -> Existing Item...
Select the file “SharedAssemblyInfo.cs” from the solution folder.
Instead of Add, click on the the arrow next to Add and click “Add as
Link”
Drag down the added linked file alongside AssemblyInfo.cs in the
same folder.
Repeat steps 1 – 4 for all projects for which you wish to add shared
assembly file.
I've tried this and it works.
First point could be solved with simple text editor that could handle several files at once and find/replace. Just open all of your csproj in it and replace string <Compile Include="Properties\AssemblyInfo.cs" /> with
<Compile Include="..\SharedAssemblyInfo.cs">
<Link>Properties\SharedAssemblyInfo.cs</Link>
</Compile>
Alternatively you could write a utility like that:
var files = Directory.GetFiles(yourSolutionDir, "*.csproj", SearchOption.AllDirectories);
foreach (var f in files) {
string contents = File.ReadAllText(f);
string result = contents.Replace("<Compile Include=\"Properties\\AssemblyInfo.cs\" />", putSecondStringHere_ItIsJustTooLong); // :)
File.WriteAllText(f, contents);
}
As for the second question... You could take a look at Visual Studio custom project templates , but I'm not sure it worth the efforts. You should IMO write test that will check this instead. It will be much simpler and outcome is actually almost the same.
UPD: About writing tests for checking solution/project files against some custom rules. Basically, sln/csproj format is simple enough to be parseable without much efforts. So if you want to have SharedAssemblyInfo.cs linked into every project - just parse csproj's and check that. Then put that checker in your build server and run it on each build. We have such system working currently and it costs something about two days to write but saved us many more (we have there more sophisticated rules and multi-solution project, so it was worth the efforts).
I won't write about this checking in detail here right now (it is not that short), but I'm going to write blog post about it soon - most probably till the end of this week. So, if you're interested - just check my blog soon :)
UPD: Here it is.
I have created an application to increment the file version automatically.
Download Applicaiton
add the following line to pre-build event command line
C:\temp\IncrementFileVersion.exe $(SolutionDir)\Properties\AssemblyInfo.cs
Build the project
To keep it simple the app only throws messages if there is an error, to confirm it worked fine you will need to check the file version in 'Assembly Information'
Note : You will have to reload the solution in Visual studio for 'Assembly Information' button to populate the fields, however your output file will have the updated version.
For suggestions and requests please email me at telson_alva#yahoo.com
This does not work for solution that has both C# and F# projects. c# project cannot reference shared f# file and vice versa.
The only option in this case is to make a separate project and refer to it from other projects

Add codebase as reference instead of copy Visual Studio

This may be a ridiculous question for you C# pros but here I go. I'm a Flash developer getting started in Silverlight and I'm trying to figure out how to create a "codebase" (a reusable set of classes) for animation. I'd like to store it in a single location and reuse it across a bunch of different projects. Normally in Flash I would add a "project path" reference and then start using the code. My question is, how do I add a folder to visual studio so that I can "use" those classes in my project. I tried "Add > Existing Item" but that copied the files into my project directory.
The easiest way would to create a new ClassLibrary project and build it. This will output a .dll file in a folder you can specify in the project settings menus, which you reference from every project that needs it.
Also, you can copy this .dll into the /bin/ folder of your project - this will do the same thing for this specific project, but when you start the next one you can change some details in the codebase library without breaking the first project.
The solution described by Tomas (adding a reference to a dll binary) is the correct solution to this problem; better than referencing the source code and compiling it into each project.
But just for extra information, if you ever do need to add a source code file to your Visual Studio project without having it make a copy of the file you can use the following steps:
Right click on your project in Solution Explorer and select Add -> Existing Item.
Navigate to the location of the source code file and select it.
On the "Add" button in the dialog window there is a drop down arrow. Click this and select "Add as Link".
This will allow you to use this source code file in your project without having VS make a copy of the file.
In Solution Explorer, right-click on the project node and click Add Reference.
In the Add Reference dialog box, select the tab indicating the type of component you want to reference. (for instance for a class library a dll)
Select the components you want to reference, then click OK.

Categories