Question

I have a few 'helper' style extension methods I use quite regularly now (they are mostly quite simple, intuitive, and work for good not evil, so please don't have this descend into a discussion around whether or not I should use them). They are largely extending core .NET CLR classes.

Currently, I have to copy the 'ExtensionMethods.cs' file that holds my extension methods to each new project within a solution to be able to use them in multiple projects.

Is it possible to define an extension to work over multiple projects within a solution, or wrap them in an 'extensions' dll, or are they confined to the scope of project?

EDIT Whilst the 'dedicated project' answers are perfectly valid, I chose marxidad's as I prefer the approach he gives. Thanks for all the answers so far, and I have upmodded them all, as they were all good answers

Was it helpful?

Solution

If you don't want to create a whole project just for the extension methods, you can link the same file into separate projects without copying the file:

  1. In Solution Explorer, select the target project.
  2. Select the Project menu.
  3. Select Add Existing Item.
  4. In the Add Existing Item dialog box, select the item you want to link.
  5. From the Open button drop-down list, select Add As Link.

OTHER TIPS

The best approach is to put them all in a single project and create a DLL. You can then include that project as a project reference or include the DLL as a binary reference (probably the better choice).

You could put your extensions in a separate project, and include that project to every new solution you're making.

Just be careful about versioning, e.g., when other applications try to make changes to your extension project then all the projects that use that method should be retested.

Scott Dorman is correct in his post too: if you don't want them changed you can compile them as a DLL library which you include in your new projects (as opposed to including an uncompiled project).

Create a project for your extensions to the .NET platform, and reference that one project in each of your application projects. It goes without saying: any and all platform stuff, and only platform stuff, goes in that one project; application stuff goes in your application projects.

You might also look into various platform libraries out there, such as Umbrella, which offer suites of extensions to the base platform.

Several answers suggests that someone would put the extension functions into a common assembly. Which is the right answer. But there's a weird thing for beginners: the IntelliSense couldn't help well enough. Let's say I extended the ObservableCollection with a ReplaceRange function / method. After a move the class of the extension functions, at first the compiler will say error CS1061: 'System.Collections.ObjectModel.ObservableCollection<WhateverDto>' does not contain a definition for 'ReplaceRange' and no extension method 'ReplaceRange' accepting a first argument of type 'System.Collections.ObjectModel.ObservableCollection<WhateverDto>' could be found (are you missing a using directive or an assembly reference?) If you then hover over the problematic ReplaceRange call, you won't get the offer to include the proper using statement automatically. Someone might think at that point that he/she did something wrong. Nothing is however, you just have to know where is your extension method, and you have to manually type the using statement for the namespace of the methods. After you got that right your source will compile.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top