【发布时间】:2013-04-08 16:00:08
【问题描述】:
为什么下面的代码给了我异常:
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 版本的。
更新
感谢下面的回答者,我已经弄清楚了:
我使用的是VS2012,但是构建服务器仍然使用VS2010的构建过程。在属性中使用空值默认参数时会出现a bug in the VS2010 / C#4 compiler。我可以通过这 3 种方式解决:
- 不要使用默认参数 -
public SearchAttribute(SearchDisplay display, string columnName) - 使用空字符串 - public SearchAttribute(SearchDisplay display, string columnName = "")
- 更新我的构建服务器。
我现在要带 2 个。 3 是我需要在其他时间考虑的事情。
【问题讨论】:
-
查看这些相关主题:stackoverflow.com/questions/8290853/… stackoverflow.com/questions/3436848/… stackoverflow.com/questions/8189807/… stackoverflow.com/questions/15048847/… 显然这是一个现已解决的错误。适用于我的 VS2010。必须安装一些您的构建服务器没有的修补程序。
标签: c# asp.net asp.net-mvc