Reputation: 5820
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?
Upvotes: 0
Views: 184
Reputation: 3391
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.
Upvotes: 1
Reputation: 100527
No. You can only restrict is attribute can be used for class, but not shape of the class.
Upvotes: 1