Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, December 5, 2006 9:40 PM
I was looking at decorating a method with the PrincipalPermission attribute. It tried to do this:
protected static string foo = "bar";
[PrincipalPermission(SecurityAction.Demand, Role = foo)]
public void DoSomething() { }
But it doesn't compile: "An attribute argument must be a constant expression, typeof expression or array creation expression"
What the technical reason for this and do you think it would be good feature to have this, or better to leave it as it is?
I would be very interested in hearing your opinions on this, thanks!
All replies (8)
Wednesday, December 6, 2006 12:32 AM ✅Answered | 1 vote
I you think of Attributes as comments that compile I think you will see why this is not allowed.
Wednesday, December 6, 2006 3:44 PM ✅Answered | 1 vote
The value of the attributes paramters must be fixed at compile time. In your example, "foo" can change at run-time.
If you change the line to
protected const string foo= "bar";
it may work.
Thursday, December 7, 2006 8:07 AM
Ok thanks for the explanation!
Thursday, February 14, 2008 3:11 PM
Some are dynamic - DebuggerDisplay for instance
http://msdn.microsoft.com/msdnmag/issues/04/05/VisualStudio2005Debugging/
Thursday, February 14, 2008 8:07 PM
DebuggerDisplayAttribute, like every other attribute, cannot take parameters at runtime. They must be compile-time constants.
DebuggerDisplayAttribute takes a const string value. Sure it parses the string at runtime and through reflection works out what you wanted it to, but that doesn't change the fact that the attribute parameter cannot be dynamic.
Friday, February 15, 2008 7:48 AM
My bad
Wednesday, May 2, 2012 12:58 PM
Hi, is there any way to use attribute parameters dynamically? Please help.
Thursday, May 3, 2012 7:20 AM
Hi, is there any way to use attribute parameters dynamically? Please help.
There is no other way than dynamically. Could you start your own thread and ask a more precise question?