相关疑难解决方法(0)

使用用户友好的字符串枚举ToString

我的枚举包含以下值:

private enum PublishStatusses{
    NotCompleted,
    Completed,
    Error
};
Run Code Online (Sandbox Code Playgroud)

我希望能够以用户友好的方式输出这些值.
我不需要能够再次从字符串变为值.

c# enums tostring

265
推荐指数
12
解决办法
24万
查看次数

从Description属性获取枚举

可能重复:
通过其描述属性查找枚举值

我有一个通用的扩展方法,它Description从以下方式获取属性Enum:

enum Animal
{
    [Description("")]
    NotSet = 0,

    [Description("Giant Panda")]
    GiantPanda = 1,

    [Description("Lesser Spotted Anteater")]
    LesserSpottedAnteater = 2
}

public static string GetDescription(this Enum value)
{            
    FieldInfo field = value.GetType().GetField(value.ToString());

    DescriptionAttribute attribute
            = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))
                as DescriptionAttribute;

    return attribute == null ? value.ToString() : attribute.Description;
}
Run Code Online (Sandbox Code Playgroud)

所以我可以......

string myAnimal = Animal.GiantPanda.GetDescription(); // = "Giant Panda"
Run Code Online (Sandbox Code Playgroud)

现在,我正试图在另一个方向上找出等效函数,比如......

Animal a = (Animal)Enum.GetValueFromDescription("Giant Panda", typeof(Animal));
Run Code Online (Sandbox Code Playgroud)

.net c# enums attributes

206
推荐指数
4
解决办法
18万
查看次数

我的枚举可以有友好的名字吗?

我有以下内容 enum

public enum myEnum
{
    ThisNameWorks, 
    This Name doesn't work
    Neither.does.this;
}
Run Code Online (Sandbox Code Playgroud)

enum带有"友好名字"的s 不可能吗?

c# enums

170
推荐指数
7
解决办法
15万
查看次数

将枚举转换为字符串

在.NET 3.5中将Enum转换为String的首选方法是什么?

  • Enum.GetName
  • Enum.Format

为什么我更喜欢其中一个呢?一个表现更好吗?

.net enums

155
推荐指数
9
解决办法
18万
查看次数

c#:如何使用枚举来存储字符串常量?

可能重复:
带字符串的枚举

可以在枚举中使用字符串常量

      enum{name1="hmmm" name2="bdidwe"}
Run Code Online (Sandbox Code Playgroud)

如果不是这样,最好的方法是什么?

我试过它不能用于字符串所以现在我将所有相关的constnats分组在一个类中

      class operation
      {
          public const string  name1="hmmm";
          public const string  name2="bdidwe"
      }
Run Code Online (Sandbox Code Playgroud)

.net string enums constants

54
推荐指数
3
解决办法
9万
查看次数

根据索引检索Enum的值 - c#

这是我的枚举:

public enum DocumentTypes
    {
        [EnumMember]
        TYPE_1 = 1,
        [EnumMember]
        TYPE_2 = 2,
        [EnumMember]
        TYPE_3 = 3,
        [EnumMember]
        TYPE_4 = 4,
        [EnumMember]
        TYPE_5 = 5,
        [EnumMember]
        TYPE_6 = 6,
        [EnumMember]
        TYPE_7 = 7,
        [EnumMember]
        TYPE_8 = 12

    }
Run Code Online (Sandbox Code Playgroud)

如果我想获得'TYPE_8',如果我只有12,有没有办法获得枚举值?

我试过这个:

((DocumentTypes[])(Enum.GetValues(typeof(DocumentTypes))))[Convert.ToInt32("3")].ToString()
Run Code Online (Sandbox Code Playgroud)

返回'TYPE_4'的值

c# enums

27
推荐指数
3
解决办法
5万
查看次数

如何在c#中本地化属性描述?

我正在开设一个将被其他国家的一些人使用的课程.我必须本地化每条消息,警告ec,以便他们能理解我们的意思.在许多情况下,我实现了我的目标.但是像描述这些属性属性在屁股中是如此痛苦.

这就是我现在所拥有的:

[Category("Editable Values"), Description("Sets the minimum select...")]
    public Ampere Simin
    {
        get
        {...}
        set
        {...}
    }
Run Code Online (Sandbox Code Playgroud)

[Category("Editable Values"), Description(Localisation.Simin)] // "Localisation" here is the internal resource file that i wrote for messages, warnings, exceptions and -unfortunately- descriptions
        public Ampere Simin
        {
            get
            {...}
            set
            {...}
        }
Run Code Online (Sandbox Code Playgroud)

这就是我想要做的.但是不可能以这种方式使用Localisations.关于我可以使用的东西的任何建议而不是它?

c# localization properties

16
推荐指数
2
解决办法
1万
查看次数

枚举可以包含字符串吗?

如何声明enum具有值的字符串?

private enum breakout {            
    page = "String1",
    column = "String2",
    pagenames = "String3",
    row = "String4"
}
Run Code Online (Sandbox Code Playgroud)

c# string enums

15
推荐指数
3
解决办法
7276
查看次数

c#中的字符串值枚举

有没有办法在c#中定义枚举如下?

public enum MyEnum : string
{
    EnGb = "en-gb",
    FaIr = "fa-ir",
    ...
}
Run Code Online (Sandbox Code Playgroud)

好的,根据erick方法和链接,我用它来检查提供的描述中的有效值:

public static bool IsValidDescription(string description)
{
    var enumType = typeof(Culture);
    foreach (Enum val in Enum.GetValues(enumType))
    {
        FieldInfo fi = enumType.GetField(val.ToString());
        AmbientValueAttribute[] attributes = (AmbientValueAttribute[])fi.GetCustomAttributes(typeof(AmbientValueAttribute), false);
        AmbientValueAttribute attr = attributes[0];
        if (attr.Value.ToString() == description)
            return true;
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

有什么改进?

.net c# enums

9
推荐指数
2
解决办法
9238
查看次数

如何将变量限制为固定的字符串集?

如果我想将数据库中spicelevel列的值限制为1、2和3,则可以执行以下操作

    private enum SpiceLevel
    {
        Low=1,
        Medium=2,
        Hot=3
    }
Run Code Online (Sandbox Code Playgroud)

然后在代码中,我可以(int)SpiceLevel.Low选择1作为香料级别。

现在,如果我需要在数据库中只接受“ Red Rose”,“ White Rose”和“ Black Rose”作为列的值,该怎么办?有什么合适的方法来解决这个问题?

我正在考虑将它们存储在配置文件或常量中,但是它们都不像枚举那样优雅。有任何想法吗?

更新:

答案在这里为我工作

c#

5
推荐指数
1
解决办法
4354
查看次数

标签 统计

c# ×8

enums ×8

.net ×4

string ×2

attributes ×1

constants ×1

localization ×1

properties ×1

tostring ×1