So I have a custom attribute, let's call it MyCustomAttribute, which has a constructor like so:

public MyCustomAttribute(int? i)
{
   // Code goes here
}

and declares a property:

public int? DefaultProperty { get; set; }

Now if I wanted to use the property, I'd need to pass in an int or null, well that's what I'd expect.

But this gives a compiler error:

[MyCustomAttribute(1, DefaultProperty = 1)]
public int? MyProperty { get; set; }

and so does this:

[MyCustomAttribute(null,DefaultProperty = null)]
public int? MyProperty { get; set; }

The error is: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type for both the constructor and the property.

Why is this? If I change the constructor to take an int, I can pass in 0, but not null, which sort of defeats the purpose of the value (which can sometimes be null)

c#
Edit
Report