为什么下面的代码给我例外C#编译器错误:引起通过传递默认字符串参数属性
CSC错误CS0182:属性参数必须是常量表达式的typeof的表达或数组创建表达式属性参数类型
在我的构建服务器上?
/// 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;
}
}
非常感谢。
令人难以置信的是,它在VS2012中建造得很好。我不确定编译器在服务器上运行的版本是什么 - 但我确信它不是2012版本。
UPDATE
多亏了下面的回答者我已经想通了这一点:
我使用VS2012,但定制服务器仍在使用Visual Studio 2010的构建过程。在属性中使用空值默认参数时会出现a bug in the VS2010/C#4 compiler。我可以解决这个3种方式:
- 不要使用默认参数 -
public SearchAttribute(SearchDisplay display, string columnName)
- 使用空字符串 - 公共SearchAttribute(SearchDisplay显示,字符串COLUMNNAME = “”)
- 更新我的构建服务器。
我刚刚和2一起去。 3是我以后需要考虑的事情。
请参阅以下相关主题:http://stackoverflow.com/questions/8290853/attribute-argument-must-be-a-constant-error-when-using-an-optional-parameter-in http:// stackoverflow .com/questions/3436848/default-value-for-attribute-constructor http://stackoverflow.com/questions/8189807/attributes-and-named-optional-constructor-parameters-not-working http://stackoverflow.com/questions/15048847/attribute-argument-must-be-a-constant-expression显然这是一个现在已经解决的错误。适用于VS2010。必须安装一些您的构建服务器没有的修补程序。 – 2013-04-08 17:01:45