An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type enum Description

痴心易碎 提交于 2019-12-23 08:26:33

问题


I am trying to have the Description of an enum pulled from the resx file, but I get the above error.

Here is my code:

public enum FinalStatus
{
    [Description(StringResources.MyStrings.Status_0)]
    Error = 0,
    [Description(StringResources.MyStrings.Status_1)]
    Ok = 1,
    [Description(StringResources.MyStrings.Status_5)]
    Warning = 2,
    [Description(StringResources.MyStrings.Status_4)]
    Unknown = 3
}

回答1:


The error is correct; these values need to be constants. You'll need to change your Status_n definitions to something more like this:

namespace StringResources{
    public class MyStrings{
        public const string Status_0 = "0";
        public const string Status_1 = "1";
        public const string Status_4 = "4";
        public const string Status_5 = "5";
    }
}


来源:https://stackoverflow.com/questions/19578368/an-attribute-argument-must-be-a-constant-expression-typeof-expression-or-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!