我有以下枚举:
public enum AuthenticationMethod
{
FORMS = 1,
WINDOWSAUTHENTICATION = 2,
SINGLESIGNON = 3
}
Run Code Online (Sandbox Code Playgroud)
然而问题是,当我要求AuthenticationMethod.FORMS而不是id 1时,我需要"FORMS"这个词.
我找到了以下解决此问题的方法(链接):
首先,我需要创建一个名为"StringValue"的自定义属性:
public class StringValue : System.Attribute
{
private readonly string _value;
public StringValue(string value)
{
_value = value;
}
public string Value
{
get { return _value; }
}
}
Run Code Online (Sandbox Code Playgroud)
然后我可以将此属性添加到我的枚举器:
public enum AuthenticationMethod
{
[StringValue("FORMS")]
FORMS = 1,
[StringValue("WINDOWS")]
WINDOWSAUTHENTICATION = 2,
[StringValue("SSO")]
SINGLESIGNON = 3
}
Run Code Online (Sandbox Code Playgroud)
当然我需要一些东西来检索StringValue:
public static class StringEnum
{
public static string GetStringValue(Enum value)
{ …Run Code Online (Sandbox Code Playgroud) 我的枚举包含以下值:
private enum PublishStatusses{
NotCompleted,
Completed,
Error
};
Run Code Online (Sandbox Code Playgroud)
我希望能够以用户友好的方式输出这些值.
我不需要能够再次从字符串变为值.
我想在我的枚举上设置空间.这是我的代码示例:
public enum category
{
goodBoy=1,
BadBoy
}
Run Code Online (Sandbox Code Playgroud)
我想设置
public enum category
{
Good Boy=1,
Bad Boy
}
Run Code Online (Sandbox Code Playgroud)
当我检索时,我想从枚举中看到Good Boy的结果
我有一个枚举,例如:
enum MyEnum
{
My_Value_1,
My_Value_2
}
Run Code Online (Sandbox Code Playgroud)
用:
comboBox1.DataSource = Enum.GetValues(typeof(MyEnum));
Run Code Online (Sandbox Code Playgroud)
但现在我的问题是:如何将"_"替换为"",以便它成为带空格而不是下划线的项目?并且数据绑定对象仍然有效
有没有办法在C#枚举常量中放置空格?我已经读过你可以通过这样做在VB中做到这一点:
Public Enum EnumWithSpaces
ConstantWithoutSpaces
[Constant With Spaces]
End Enum
Run Code Online (Sandbox Code Playgroud)
...然后像这样访问它:
Public Sub UsingEnumWithSpaces()
Dim foo As EnumWithSpaces = EnumWithSpaces.[Constant With Spaces]
End Sub
Run Code Online (Sandbox Code Playgroud)
这意味着CLR可以处理带空格的枚举.
有没有办法在C#中做到这一点?
我正在尝试了解如何在阅读我的其他问题的答案后使用类型转换器.但我不确定我是否完全明白了......
在我的特定情况下,我想通过根据枚举成员获取资源字符串来将枚举成员"转换"为本地化字符串.所以,例如,如果我有这个枚举:
public enum Severity
{
Critical,
High,
Medium,
Low
}
Run Code Online (Sandbox Code Playgroud)
或这个:
public enum Color
{
Black = 0x0,
Red = 0x1,
Green = 0x2,
Blue = 0x4,
Cyan = Green | Blue,
Magenta = Red | Blue,
Yellow = Red | Green,
White = Red | Green | Blue,
}
Run Code Online (Sandbox Code Playgroud)
如何创建可以将这些成员转换为本地化字符串的类型转换器?我将如何使用它?目前我需要在WinForms应用程序中使用它,但也欢迎更一般的例子.
我有一个带有注释的枚举,EnumMember以促进类似于以下内容的JSON.NET序列化:
[DataContract]
[JsonConverter(typeof(StringEnumConverter))]
public enum Status
{
[EnumMember(Value = "NOT_ADMITTED")]
NotAdmitted,
[EnumMember(Value = "ADMITTED")]
Admitted
}
Run Code Online (Sandbox Code Playgroud)
现在,独立于JSON.NET序列化,我想将枚举实例转换为字符串,同时遵守EnumMember数据协定中的注释,例如:
aStatusInstance.ToString() == "NOT_ADMITTED"。
有什么建议么?谢谢!
更新:我的解决方案
我修改了接受的答案中的代码,以创建扩展方法来检索EnumMember值:
public static string GetEnumMemberValue(this Enum enumValue)
{
var type = enumValue.GetType();
var info = type.GetField(enumValue.ToString());
var da = (EnumMemberAttribute[])(info.GetCustomAttributes(typeof(EnumMemberAttribute), false));
if (da.Length > 0)
return da[0].Value;
else
return string.Empty;
}
Run Code Online (Sandbox Code Playgroud) 我有这样的枚举
enum Animal:byte
{
Cat=0,
Dog=1,
Horse=2
}
Run Code Online (Sandbox Code Playgroud)
我想覆盖ToString()它来编写一个自定义字符串,因为我的应用程序是一个多语言的(Cat.ToString()应该检索cat的翻译单词).所以Descriptionkeyowrd不能在这里使用.任何人都给我一个提示,找到一个简洁的方法来解决这个问题?
我需要一个枚举或类似的东西来做这样的事情:
public enum MyStringEnum {[StringValue("Foo A")] Foo ="A",[StringValue("Foo B")] Foo ="B"}
这可能吗?我的例子,我返回一个数据集,其值表示为A,B,C,D,E ..我需要一个解决方案将其作为字符串表示返回?
我想显而易见的是创建一个扩展方法或只有一个switch语句并返回一个字符串的东西..任何其他更清洁的解决方案?
亲爱的,戴夫
我们有一些东西可以导出成各种格式.目前我们有这样的格式由枚举表示如下:
[Flags]
public enum ExportFormat
{
None = 0x0,
Csv = 0x1,
Tsv = 0x2,
Excel = 0x4,
All = Excel | Csv | Tsv
}
Run Code Online (Sandbox Code Playgroud)
问题是必须枚举这些,并且它们还需要在ui中进行翻译或描述.目前我通过创建两个扩展方法解决了这个问题 他们工作,但我真的不喜欢他们或解决方案......他们觉得有点臭.问题是我真的不知道如何做得更好.有没有人有任何好的选择?这是两种方法:
public static IEnumerable<ExportFormat> Formats(this ExportFormat exportFormats)
{
foreach (ExportFormat e in Enum.GetValues(typeof (ExportFormat)))
{
if (e == ExportFormat.None || e == ExportFormat.All)
continue;
if ((exportFormats & e) == e)
yield return e;
}
}
public static string Describe(this ExportFormat e)
{
var r = new List<string>();
if ((e & ExportFormat.Csv) == ExportFormat.Csv) …Run Code Online (Sandbox Code Playgroud) c# ×10
enums ×7
.net-4.0 ×1
clr ×1
datacontract ×1
json.net ×1
localization ×1
tostring ×1
vb.net ×1