-1

I need to declare an attribute for coverage exclusion in my code, the issue is that i have a project group and i wish to create it somewhere where i can access it from all projects when i need it, right now i have it outside of the namespaces so it would be easier to use, and its declared in each project like:

public class CoverageExcludeAttribute : Attribute
{

}

is there any better way to achieve this goal in a way it could be access anywhere in my project group and declared only once, without having to add its namespace (e.g by using the global namespace) to each file i use the attribute in?

Thank you

P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
  • 1
    have you thought about just creating a CustomCoverageExclusion Class as a separate .cs file or .dll that you can reference within your project.. you could add this as properties internal to the project have a look at Settings – MethodMan Aug 17 '12 at 13:27
  • 1
    -1 `without having to add its namespace to each file i use the attribute in?`. .NET intentionally requires the namespace to be imported via a using statement or by fully qualifying the name. Even the System namespace does not exist by default. To ask for .NET to stop working like this is futile. – P.Brian.Mackey Aug 17 '12 at 13:37
  • 1
    While I actually agree with P.Brian.Mackey, I think the only way to do it is exactly as DjKraze said: create a new micro-project of type ClassLibrary, add a single .cs file with your Coverage(..) class and ensure that class is inside no namespaces block. Build and then in each one of the other projects do a Add-Reference to that micro-project you just created.. Sorry, almost no other options for such thing! – quetzalcoatl Aug 17 '12 at 13:42
  • @P.Brian.Mackey ur a great help dude, with -1 one get really motivated to ask here no? anyway i have my class outside the namespace in the project then its part of the default namespace (global) so i can access it in the same assembly its declard in. – CloudyMarble Aug 17 '12 at 13:46
  • @quetzalcoatl thank you for your answer, its kind of overdesigne to do this for one attribute, would you post it as an answer so i can accep it. thanx again. – CloudyMarble Aug 17 '12 at 13:49
  • @O.D I edited the question with your updated information regarding global namespaces and removed the downvote. – P.Brian.Mackey Aug 17 '12 at 13:50
  • Done. Also, I've added few other options, but they will not work without some serious .bat/ruby/perl/etc scripts.. – quetzalcoatl Aug 17 '12 at 13:58

3 Answers3

1

While I actually agree with P.Brian.Mackey, I think the only way to do it is exactly as DjKraze said:

Create a new micro-project of type ClassLibrary, add a single .cs file with your Coverage(..) class and ensure that class is inside no namespaces block. Then build it and for each one of the other projects do a Add-Reference to that micro-project you just created.. That way it will surely work, and you will have a handy place to put any further 'common code' to be available everywhere.

However, each project will have to be updated with the reference. This is the minimum requirement - all in all, if you want to use anything instead of copying, it must be referred..

Sorry, almost no other options for such thing! The other way is to .. ugh, copy. You can easily set up a simple pre-build script that will copy given .cs file to each one of your projects, but "adding" the file to the .csproj's build list is a bit harder, still possible with use of some Ruby or Python or friends...

Hm.. saying that, It may be possible to write a pre-build script to inject a reference to the micro-project automatically.. But I wont know if this is worth doing. Do you have more than 50-100 projects? Else, probably it's not worth..

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
0

This only applies to VS2010 and above

If you want some source code defined in each of your projects, but without a project reference, take a look at some of the functionality provided by NuGet, especially Source Code Transformations. These allow the addition of some source code to the project when you add the NuGet package to the project.

Lukazoid
  • 19,016
  • 3
  • 62
  • 85
-1

You can use Dependency Injection http://en.wikipedia.org/wiki/Dependency_injection

The most popular are: Microsoft Unity, Ninject, NHibernate, StructureMap, Autofac.

Good luck!