Why does the code below give me the exception:
CSC error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
On my build server?
/// Customer.cs...
[Search(SearchAttribute.SearchDisplay.Regular)]
public Category Category
{
get; set;
}
public enum Category : byte
{
X = 0x01,
Y = 0x02,
...
}
/// SearchAttribute.cs...
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class SearchAttribute : Attribute
{
public SearchDisplay Display { get; private set; }
public enum SearchDisplay
{
None = (byte) 0x01,
Regular = (byte) 0x02
}
public SearchAttribute(SearchDisplay display, string columnName = null)
: base()
{
Display = display;
}
}
Many Thanks.
Infuriatingly, it builds fine in VS2012. I'm unsure what version of the compiler runs on the server - but I'm fairly sure it's not the 2012 one.
UPDATE
Thanks to the answerers below I've figured this out:
I am using VS2012, but the build server is still using the VS2010 build process. There is a bug in the VS2010 / C#4 compiler that occurs when a null valued default parameter is used in an attribute. I can get around this 3 ways:
- Don't use a default parameter -
public SearchAttribute(SearchDisplay display, string columnName)
- Use a empty string - public SearchAttribute(SearchDisplay display, string columnName = "")
- Update my build server.
I am going with 2 just now. 3 is something that I will need to think about at another time.