属性参数必须在属性的构造函数使用一个可选的参数时,是一个常量错误 [英] Attribute argument must be a constant error when using an optional parameter in the attribute constructor

查看:140
本文介绍了属性参数必须在属性的构造函数使用一个可选的参数时,是一个常量错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释为什么这code工作:

Can anyone explain why this code works:

public class AdministratorSettingValidationAttribute : Attribute
{
    public AdministratorSettingValidationAttribute(AdministratorSettingDataType administratorSettingDataType)
    {
        DataType = administratorSettingDataType;
    }

    public AdministratorSettingValidationAttribute(AdministratorSettingDataType administratorSettingDataType, Type enumerationType)
    {
        DataType = administratorSettingDataType;
        EnumerationType = enumerationType;
    }
}

...但重构它使用一个可选参数来代替:

...but refactoring it to use an optional parameter instead:

    public AdministratorSettingValidationAttribute(AdministratorSettingDataType administratorSettingDataType, Type enumerationType = null)
    {
        DataType = administratorSettingDataType;
        EnumerationType = enumerationType;
    }

...导致编译时错误: 的属性参数必须是一个常量前pression,属性参数类型的typeof EX pression或数组创建EX pression

...causes a compile time error: "An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type".

推荐答案

该错误报道在去年七月已经是固定的。此修复程序会出现在C#中的下一个版本。有关详细信息,请参阅该连接反馈项目:

UPDATE

The bug was reported in July of last year and is already fixed. The fix will appear in the next version of C#. See this Connect feedback item for details:

<一个href="http://connect.microsoft.com/VisualStudio/feedback/details/574497/optional-parameter-of-type-string-in-a-attribute-constructor-cannot-be-null">http://connect.microsoft.com/VisualStudio/feedback/details/574497/optional-parameter-of-type-string-in-a-attribute-constructor-cannot-be-null

这显然是一个编译器错误。谢谢你把它给我的注意。

That's clearly a compiler bug. Thanks for bringing it to my attention.

应该怎么在这里发生的编译器应该认识到,可选值前pression隐式转换为正式参数类型,然后把前pression作为一个恒定的前pression该类型的。它实际上是什么做的是治疗前pression为无类型null文本,这是不对的。

What is supposed to happen here is the compiler is supposed to realize that the optional value expression is implicitly converted to the formal parameter type, and then treat the expression as a constant expression of that type. What it is actually doing is treating the expression as the typeless null literal, which is wrong.

您可以解决错误通过转动恒成一个显式类型之一:

You can work around the bug by turning the constant into an explicitly typed one:

public AdministratorSettingValidationAttribute(AdministratorSettingDataType administratorSettingDataType, Type enumerationType = (Type)null) 

解决方法是可能简单,但我不能保证该修复程序将在C#中的下一个版本;我不知道什么样的计划就像是采取非关键bug修复了这一点。

The fix is probably straightforward but I cannot promise that the fix will be in the next version of C#; I'm not sure what the schedule is like for taking non-critical bug fixes at this point.

再次感谢和歉意的不便。

Thanks again, and apologies for the inconvenience.

这篇关于属性参数必须在属性的构造函数使用一个可选的参数时,是一个常量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆