0

I have written a custom credit card validation attribute that checks CardNumber property is valid for a particular card type (another property in the same class)

[CardValidationBinCheck(this.CardType, 
                        ErrorMessage = "CreditCardNumberDoesNotMatchCardType")]
public string CardNumber
{
 ...
}

This won't compile as studio complains that attribute arguments must be constant, a typeof expression or an array creation expression of an attribute parameter type (?).

Is there anyway I can pass cirvumvent this and pass my this.CardType to the attribute?

Kindness,

Dan

4

1 に答える 1

5

残念ながら違います。ILで属性値をメンバー参照トークンにすることが許可されている場合でも(たとえば、PropertyInfo効果的に)、C#演算子を使用して属性値を取得することはできません1。それを文字列リテラルとして渡してから、リフレクションを使用して取得することができPropertyInfoます。醜くて壊れやすいですが、おそらくあなたが得る最も近いものです。


1これはよく知られた機能要求であり、「infoof」演算子と呼ばれることもあります。しかし、それが実装されている兆候はありません。

于 2009-09-14T13:32:48.847 に答える