RE: Attribute Expression Must be a Constant - Problem

From: Yan-Hong Huang[MSFT] (yhhuang_at_online.microsoft.com)
Date: 02/25/04


Date: Wed, 25 Feb 2004 09:35:17 GMT

Hello,

Thanks for posting in the group.

Based on the description, now you are adding attributes like
FileIOPermissionAttribute( SecurityAction.RequestMinimum,
Read=System.IO.Directory.GetCurrentDirectory() ) into assemblyinfo.cs
file. When you build the project, the compiler gives error message "An
attribute argument must be a constant expression, typeof expression or
array creation expression". Please feel free to post here if I have
misunderstood the problem.

I am afraid it is impossible to use a runtime expression in Attribute
arguments.

The instance of an attribute class is constructed by the compiler when the
compile detects an attribute applied to a target, and at this time the
assembly hasn't been completed yet. And then the compiler initializes any
public fields and properties have been specified. After that, the instance
of the attribute is serialized to the target's metadata table entry.

Please Note: all these are done by the compiler and there is not a way to
get any runtime variable. Even if you use typeof keyword, you can only get
those Types can be retrieved at compile time. To obtain the run-time type
of an expression, you can use the .NET Framework method GetType.

In order to achieve what you want, we need to do it programmatically, not
declaratively. Please refer to the following article on how to do it:
"Security in .NET: Enforce Code Access Rights with the Common Language
Runtime "
http://msdn.microsoft.com/msdnmag/issues/01/02/CAS/default.aspx

Hope that helps.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.



Relevant Pages

  • Re: Why does the copy constructor get called?
    ... >>posting for a while. ... It looks like the compiler is attempting to copy the ... >>the explicit copy ctor). ... the compiler is permitted to generate a copy of that rvalue ...
    (comp.lang.cpp)
  • Re: switch error
    ... These are all ansi c using VS 2005 compiler. ... This is a code fragment. ... (If you used tabs for indentation, ... please expand them to spaces before posting; ...
    (comp.lang.c)
  • Re: sci.crypt Sanbox here i am ;)
    ... >> I'm going to stop posting in this thread now, ... >> comment was a throwaway one meant to show the OP that his code was ... Any C compiler is required to issue diagnostics for the ... since it has so many problems that the language standard ...
    (sci.crypt)
  • Re: end-of-file during read using unformatted reading
    ... F77/F90 compiler, ... about in your posting had nothing to do with the reopen. ... As far as I can tell, you must be having as hard a time figuring out ... observable relationship with the OP's question or anything else ...
    (comp.lang.fortran)