I'm fond of the new .NET 4.0 optional parameters feature, but I've recently came across a limitation I do not understand.
Consider the following simple attribute:
class SomeAttribute : Attribute
public SomeAttribute(string someString = null)
When I try to use the attribute in the statement
, the following error is generated on compiling:
"An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type."
This strikes me as odd, since the default argument IS a constant expression.... And even stranger is the following:
The code can compile when I change
, or when I remove the part
, and call for instance
. I'm clueless....
Can someone explain me why this error is generated and not in the other case?
And are there any workarounds, besides creating all sorts of constructor overload(like in .NET versions before 4.0)?