一个属性参数必须是恒定的前pression,的属性参数类型枚举描述的typeof EX pression或数组创建EX pression [英] An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type enum Description

查看:156
本文介绍了一个属性参数必须是恒定的前pression,的属性参数类型枚举描述的typeof EX pression或数组创建EX pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个枚举的描述从RESX文件拉升,但我得到上述错误。

下面是我的code:

 公开枚举FinalStatus
{
    [说明(StringResources.MyStrings.Status_0)
    错误= 0,
    [说明(StringResources.MyStrings.Status_1)
    OK = 1,
    [说明(StringResources.MyStrings.Status_5)
    警告= 2,
    [说明(StringResources.MyStrings.Status_4)
    未知= 3
}
 

解决方案

该错误是正确的;这些值需要是常数。你需要改变你的 Status_n 定义,以更多的东西是这样的:

 命名空间StringResources {
    公共类MyStrings {
        公共常量字符串Status_0 =0;
        公共常量字符串Status_1 =1;
        公共常量字符串Status_4 =4;
        公共常量字符串Status_5 =5;
    }
}
 

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
}

解决方案

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";
    }
}

这篇关于一个属性参数必须是恒定的前pression,的属性参数类型枚举描述的typeof EX pression或数组创建EX pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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