开发者

Limiting a typeof() parameter on an attribute to specific types only...is this possible?

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 : Supe开发者_Go百科rSpecialBaseException" but of course, this is not generics (joyful Attribute limitation)


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜