I saw this feature in Visual Studio again the other day and honestly I had kind of forgotten about it. After discussing it with some colleagues, most of them didn’t even know the feature existed. What I am talking about is the ability to create a link between files in Visual Studio. What this allows you to do is actually create a link to a file instead of just making a copy. Now, I don’t want to get into a discussion of why you would never want to do this or how this is not a proper way to implement code reuse. Keep in mind though that you can use it to link other types of non-code files such as an XML file. However, for today’s purpose, I’m just going to link a class into another project. Consider my following example with two class libraries. I want to reuse Class1.cs inside ClassLibrary2.
I have a simple class that looks like this.
public class Class1
public void DoSomething()
int x = 5;
This class clearly has so much valuable code in it that I have to reuse it in ClassLibrary2, but not make a reference to ClassLibrary2. This is where the Add as Link functionality comes in. The process is simple, using the Add Existing Item menu on the project’s context menu. Navigate to the existing item in the other class library and then make note of the arrow next to the Add button, click on it and choose Add as Link instead.
Once you do that you will have a link to the file in your other class library. You can open it and edit it as normal from the linked location, but it will actually edit the file link. You can tell it is linked in Solution Explorer by the icon.
Notice the link icon on Class1.cs in ClassLibrary2. If you click on the linked file, you can see the path to the file it has linked in the properties (although its cut off here in my screenshot).
Once you are done here, you can make changes to the file from either class library and the original file gets updated. Keep in mind though, if you delete the original file, the link will be broken and you will get an error when trying to view any links. I don’t know how useful this feature will be to all of you, but I think there is a time and place for everything. I can definitely see this being useful in some cases. This isn’t a new feature either. I think its been around since at least Visual Studio 2003, but I could be wrong.