0

Given the following situation:

I do have a custom attribute that I can place on a class. (It means a Singleton, so any class using this attribute can be instantiated as a Singleton).

But of course, for a good Singleton, you may not create a new instance of the object by using the new() function in .NET

Is it therefore possible to throw a custom build error when a class that implements this attribute has a public constructor?

user2864740
  • 60,010
  • 15
  • 145
  • 220
Complexity
  • 5,682
  • 6
  • 41
  • 84
  • See [this answer](http://stackoverflow.com/a/154254/1726343) for how to generate compiler warnings. Not sure if compiler errors are possible. – Asad Saeeduddin May 21 '14 at 07:26
  • Thanks, I knew how to throw compiler warnings, but not how to throw errors. However, I don't think it's even possible. – Complexity May 21 '14 at 07:28
  • Note that your title is duplicate of http://stackoverflow.com/questions/6266031/c-sharp-net-how-to-throw-a-compiler-error?rq=1 , but your body of the post discuss totally different requirements. Consider editing... – Alexei Levenkov May 21 '14 at 07:29
  • 1
    @Asad The [Obsolete Attribute](http://msdn.microsoft.com/en-us/library/961hff5d(v=vs.110).aspx) that is used in your link can be used to throw errors (there is a bool parameter to set to true). However the class is sealed and so I don't know how you could use it to first check for the existence of a public constructor. – Ben May 21 '14 at 07:35
  • Indeed, I've seen an error can be throwed, but you can just place it on a method or something. I can't add a constraint to say that a class implementing an attribute should not have a public constructor. – Complexity May 21 '14 at 07:37
  • 2
    If you can teach your teammembers to add a Singleton attribute to a singleton class, you can also teach them not to use a public constructor. Also, if they want to break your design, removing an attribute is not that hard. A proper code reviewing process would be more effective here. – MarkO May 21 '14 at 08:04
  • Thanks for this valuable answer. However, as explained below, I can write a unit test that checks all types in an assembly for the the attribute and check if it has a public class (it's done through reflection though so it will not be that fast). – Complexity May 21 '14 at 08:06
  • 1
    You might be able to write a [custom code analysis rule](http://blogs.msdn.com/b/codeanalysis/archive/2010/03/26/how-to-write-custom-static-code-analysis-rules-and-integrate-them-into-visual-studio-2010.aspx) to do this. (More info [here](http://www.binarycoder.net/fxcop/index.html)) – Matthew Watson May 21 '14 at 08:11
  • My "recommendation" is to *not* have a singleton class, but rather use a DI container that exposes a singleton instance (I have very few static access sites in my projects) – user2864740 May 21 '14 at 08:13

2 Answers2

1

No. You can only restrict is attribute can be used for class, but not shape of the class.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
1

I would suggest that you are asking the compiler to implement some logical testing of the design of your code. However, I think the compiler's main purpose is only to check the integrity of the code and so perhaps it would become confusing to have it also check design characteristics as well.

I would suggest that an alternative design should be considered. E.g. using a base class for all singletons: See this post or this post.

Alternatively, in the past I have also used unit tests to check that code has been structured correctly. Here is an unrelated answer that might give you some ideas of how to apply this to your situation.

Community
  • 1
  • 1
Ben
  • 3,241
  • 4
  • 35
  • 49
  • Hello, thanks for your answer, but I don't want to change the design because my Singleton design allows me to even make internal .NET classes (no source available and sealed) behave themselves as a Singleton. The only way I can think of is a Unit Test that loads up all the assemblies through reflection and check for every type if it has the Singleton attribute and if that's the case check if the class does have a public constructor. If that's the case, fail the Unit Test. What do you think about this approach? – Complexity May 21 '14 at 08:05
  • The unit test approach is perhaps a last resort. (Not even sure if 'unit test' is the right name for this type of test). But in the absence of a better suggestion, I would say try it out. Sounds like you have the right idea. – Ben May 21 '14 at 08:16