views:

47

answers:

3

I'm using attribute-based validation, and I would like to limit what can be entered in theses attributes.

ex:

ThisValidatesSomethingAttribute(Type typeOfExceptionToThrowOnFailure)

so on use it is

[ThisValidatesSomething(typeof(MassiveFailureException))]
public int SomeIntParameter()

is there a way to limit the types that can be entered in this attribute?

I want to limit the Type parameter to be only types that derive from perhaps say a base class named "SuperSpecialBaseException"

In generics I would just use a type constraint "where T : SuperSpecialBaseException" but of course, this is not generics (joyful Attribute limitation)

A: 

Only at runtime.

Kent Boogaart
+1  A: 

Not as a compile-time feature. That's equivalent to only being able to accept int parameters between 10 and 20; this sort of check has to be performed in your code, it can't be declared.

Adam Robinson
A: 

No there is not a way to do this at compile time. You'll need to do runtime validation of the parameter.

JaredPar