David
David

Reputation: 16120

c#: what is a constant expression?

I'm working with attributes at the moment. I often run into the error 'An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type.' I don't really know what 'constant expression' means.

It would have been really useful to be able to pass in a Func<MyType, bool> to the attribute (to be consumed by the code which executes when the attribute is present). But alas, no. I don't understand why that type cannot be placed in assembly metadata, which I assume is the reason I cannot pass it into the attribute.

Can anyone give me any ideas?

Upvotes: 8

Views: 9565

Answers (4)

Tim M.
Tim M.

Reputation: 54359

A constant expression is an expression that shall be fully evaluated at compile-time.

A constant expression may be either a value type or a reference type. If a constant expression is a value type, it must be one of the following types: sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, or any enumeration type. If a constant expression is a reference type, it must be the string type, a default value expression (§12.8.20) for some reference type, or the value of the expression must be null.

Source

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500055

Constant expressions are values determined solely at compile-time, including string concatenation of other constant expressions, arithmetic etc.

So for example "" is a constant expression, but String.Empty isn't.

String is the only reference type to support a non-null constant expression. For value types, the primitive types (int etc) and decimal support constant expressions... although you can't use decimal in attributes, as it's not a primitive in the CLR. (You can't even specify decimal as a parameter type in an attribute constructor.)

See section 7.19 of the C# 4 spec for more information.

Upvotes: 11

Botz3000
Botz3000

Reputation: 39600

The compiler needs to be able to create the Attributes at compile time, since they are embedded in your assembly with their actual data (they are instantiated by the compiler and serialized into the output file). That's why you need a constant expression.

Basically you can use all the basic data types (like int, bool, string etc.). You can also use typeof expressions because they will be resolved to metadata tokens identifying a type, which is fine at compile time.

Maybe you can put the concept of your Func<MyType, bool> into an interface that your types implement. Or into a separate handler class that you can pass to your Attribute by using a typeof(MyHandlerClass) expression.

Upvotes: 5

Tigran
Tigran

Reputation: 62248

Attributes in C# are compile time artifacts, so in order to be created they have to have a constant type parameters/markers/whatever...

There are possible attempts to do what you are, I suppose, going to do:

Dynamic Attributes in C#

Upvotes: 2

Related Questions