I'm looking for a way to create a class that has a default public constructor, but that prohibits it's use outside of reflection.
In more detail, I'm using an object persistence framework (AWS DynamoDB - I don't have the option to change) that requires objects implement such a public default constructor. The framework also requires all persisted fields and properties to also have public get and set accessors and uses them when creating and initializing a new object instance. this leads to problems because the accessors need to serve dual purposes - they need to be "bare" when used by the framework, but enforce restrictions and validation when used by code. In any case, I've got that part worked out, but it relies on never allowing code to construct a persisted object using the default constructor (the one the persistence framework uses).
Back to the original question - to make this all work, I need to prohibit code from using the default constructor while still making it available for use by the persistence framework (which searches only for an empty public constructor). I started off by using a static flag and setting it before and after any potential object persistence uses and throwing an exception in the default constructor if it wasn't set, but this started to cause problems in a multi-threaded environment (a web server). One thread would set it for use by the persistence framework which would open up the constructors for use by other threads. I also tried a ThreadStatic flag, but that had the opposite problem of preventing construction by the framework since it sometimes spawns worker threads.
Now I'm wondering if there is actually a way of doing this. At this point it's become more of a curiosity than an requirement; I can always just write a comment along the lines of "DON'T USE THIS CONSTRUCTOR", but enforcing the rule directly would make me feel better. I've used PostSharp before and perhaps there is a way to use an aspect for compile-time validation to prevent code-based default constructor use? Any other suggestions?
Using PostSharp, you could use an architectural constraint to validate that some constructor is not called from user code (do a ReferentialConstraint and attach the constraint to the class, possibly using multicasting and inheritance). Basically, if the only legitimate use of this code if from reflection, you could emit an error if you find any reference to the constructor, expect chaining from other constructors.
Note that this would not prevent the constructor to be called from the new T()
construct, where T is a generic type parameter. So only forbid the new operators: from your ReferentialAspect, use ReflectionSearch.GetMethodsUsingDeclaration and filter for (Instructions & MethodUsageInstructions.NewObject) != 0
.
EDIT: added details about ReflectionSearch.GetMethodsUsingDeclaration and MethodUsageInstructions.