我知道以下是不可能的,因为它必须是一个int
enum GroupTypes
{
TheGroup = "OEM",
TheOtherGroup = "CMB"
}
Run Code Online (Sandbox Code Playgroud)
从我的数据库中我得到一个包含不全面代码的字段(OEM和CMB).我想把这个领域变成一个枚举或其他可以理解的东西.因为目标是可读性,所以解决方案应该简洁.
我还有其他选择吗?
Eve*_*ien 341
我喜欢在类中使用属性而不是方法,因为它们看起来更像枚举.
以下是Logger的示例:
public class LogCategory
{
private LogCategory(string value) { Value = value; }
public string Value { get; set; }
public static LogCategory Trace { get { return new LogCategory("Trace"); } }
public static LogCategory Debug { get { return new LogCategory("Debug"); } }
public static LogCategory Info { get { return new LogCategory("Info"); } }
public static LogCategory Warning { get { return new LogCategory("Warning"); } }
public static LogCategory Error { get { return new LogCategory("Error"); } }
}
Run Code Online (Sandbox Code Playgroud)
将类型安全的字符串值作为参数传递:
public static void Write(string message, LogCategory logCategory)
{
var log = new LogEntry { Message = message };
Logger.Write(log, logCategory.Value);
}
Run Code Online (Sandbox Code Playgroud)
用法:
Logger.Write("This is almost like an enum.", LogCategory.Info);
Run Code Online (Sandbox Code Playgroud)
Gle*_*lar 154
您还可以使用扩展模型:
public enum MyEnum
{
[Description("String 1")]
V1= 1,
[Description("String 2")]
V2= 2
}
Run Code Online (Sandbox Code Playgroud)
您的扩展类
public static class MyEnumExtensions
{
public static string ToDescriptionString(this MyEnum val)
{
DescriptionAttribute[] attributes = (DescriptionAttribute[])val
.GetType()
.GetField(val.ToString())
.GetCustomAttributes(typeof(DescriptionAttribute), false);
return attributes.Length > 0 ? attributes[0].Description : string.Empty;
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
MyEnum myLocal = MyEnum.V1;
print(myLocal.ToDescriptionString());
Run Code Online (Sandbox Code Playgroud)
rpa*_*abi 83
如何使用带常量的静态类?客户端代码看起来与枚举没有什么不同.
static class GroupTypes
{
public const string TheGroup = "OEM";
public const string TheOtherGroup = "CMB";
}
void DoSomething(string groupType)
{
if(groupType == GroupTypes.TheGroup)
{
// Be nice
}
else if (groupType == GroupTypes.TheOtherGroup)
{
// Continue to be nice
}
else
{
// unexpected, throw exception?
}
}
Run Code Online (Sandbox Code Playgroud)
cas*_*One 28
您可以向枚举中的项添加属性,然后使用反射从属性中获取值.
您必须使用"field"说明符来应用属性,如下所示:
enum GroupTypes
{
[field:Description("OEM")]
TheGroup,
[field:Description("CMB")]
TheOtherGroup
}
Run Code Online (Sandbox Code Playgroud)
然后,您将反映枚举类型的静态字段(在本例中为GroupTypes),并DescriptionAttribute使用反射获取您要查找的值:
public static DescriptionAttribute GetEnumDescriptionAttribute<T>(
this T value) where T : struct
{
// The type of the enum, it will be reused.
Type type = typeof(T);
// If T is not an enum, get out.
if (!type.IsEnum)
throw new InvalidOperationException(
"The type parameter T must be an enum type.");
// If the value isn't defined throw an exception.
if (!Enum.IsDefined(type, value))
throw new InvalidEnumArgumentException(
"value", Convert.ToInt32(value), type);
// Get the static field for the value.
FieldInfo fi = type.GetField(value.ToString(),
BindingFlags.Static | BindingFlags.Public);
// Get the description attribute, if there is one.
return fi.GetCustomAttributes(typeof(DescriptionAttribute), true).
Cast<DescriptionAttribute>().SingleOrDefault();
}
Run Code Online (Sandbox Code Playgroud)
DescriptionAttribute如果您希望能够确定属性是否应用,我选择返回上面的内容.
小智 24
我使用了先前答案中提到的结构,但消除了任何复杂性。对我来说,这最像创建一个字符串枚举。它的使用方式与使用枚举的方式相同。
struct ViewTypes
{
public const string View1 = "Whatever string you like";
public const string View2 = "another string";
}
Run Code Online (Sandbox Code Playgroud)
使用示例:
switch( some_string_variable )
{
case ViewTypes.View1: /* do something */ break;
case ViewTypes.View2: /* do something else */ break;
}
Run Code Online (Sandbox Code Playgroud)
小智 22
你可以很容易地做到这一点.使用以下代码.
enum GroupTypes
{
OEM,
CMB
};
Run Code Online (Sandbox Code Playgroud)
然后,当您想要获取每个枚举元素的字符串值时,只需使用以下代码行.
String oemString = Enum.GetName(typeof(GroupTypes), GroupTypes.OEM);
Run Code Online (Sandbox Code Playgroud)
我以前成功地使用过这个方法,并且我还使用了一个常量类来保存字符串常量,两者都运行得很好,但我更喜欢这个.
dar*_*asd 17
尝试将常量添加到静态类.你没有最终得到一个类型,但你有可读的,有组织的常量:
public static class GroupTypes {
public const string TheGroup = "OEM";
public const string TheOtherGroup = "CMB";
}
Run Code Online (Sandbox Code Playgroud)
Dav*_*nde 13
为包含以下内容的数据库创建第二个枚举:
enum DBGroupTypes
{
OEM = 0,
CMB = 1
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以使用Enum.Parse从字符串"OEM"和"CMB"中检索正确的DBGroupTypes值.然后,您可以将这些转换为int,并从您希望在模型中进一步使用的右枚举中检索正确的值.
C. *_*oss 13
使用课程.
编辑:更好的例子
class StarshipType
{
private string _Name;
private static List<StarshipType> _StarshipTypes = new List<StarshipType>();
public static readonly StarshipType Ultralight = new StarshipType("Ultralight");
public static readonly StarshipType Light = new StarshipType("Light");
public static readonly StarshipType Mediumweight = new StarshipType("Mediumweight");
public static readonly StarshipType Heavy = new StarshipType("Heavy");
public static readonly StarshipType Superheavy = new StarshipType("Superheavy");
public string Name
{
get { return _Name; }
private set { _Name = value; }
}
public static IList<StarshipType> StarshipTypes
{
get { return _StarshipTypes; }
}
private StarshipType(string name, int systemRatio)
{
Name = name;
_StarshipTypes.Add(this);
}
public static StarshipType Parse(string toParse)
{
foreach (StarshipType s in StarshipTypes)
{
if (toParse == s.Name)
return s;
}
throw new FormatException("Could not parse string.");
}
}
Run Code Online (Sandbox Code Playgroud)
use*_*664 10
.Net Core 3.0/C# 8.0中的新功能(如果您的工作环境允许您升级项目)是一个看起来有点枚举的简写 switch 语句。归根结底,这还是我们多年来一直使用的旧的、无聊的 switch 语句。
这里唯一真正的区别是 switch 语句有了新的套装。
public static RGBColor FromRainbow(Rainbow colorBand) =>
colorBand switch
{
Rainbow.Red => new RGBColor(0xFF, 0x00, 0x00),
Rainbow.Orange => new RGBColor(0xFF, 0x7F, 0x00),
Rainbow.Yellow => new RGBColor(0xFF, 0xFF, 0x00),
Rainbow.Green => new RGBColor(0x00, 0xFF, 0x00),
Rainbow.Blue => new RGBColor(0x00, 0x00, 0xFF),
Rainbow.Indigo => new RGBColor(0x4B, 0x00, 0x82),
Rainbow.Violet => new RGBColor(0x94, 0x00, 0xD3),
_ => throw new ArgumentException(message: "invalid enum value", paramName: nameof(colorBand)),
};
Run Code Online (Sandbox Code Playgroud)
您会注意到,我从此处复制的上面的代码实际上使用枚举作为参数。
这并不完全是你想要的(相信我,我长期以来一直想要类似于OP所要求的东西),但我实际上觉得这在某种程度上是来自MS的橄榄枝。JMO。
希望它能帮助别人!
摘自@EvenMien 并添加了一些评论。(也适合我自己的用例)
public struct AgentAction
{
private AgentAction(string value) { Value = value; }
public string Value { get; private set; }
public override string ToString() { return this.Value; }
public static AgentAction Login = new AgentAction("Login");
public static AgentAction Logout = new AgentAction("Logout");
public static implicit operator string(AgentAction action) { return action.ToString(); }
}
Run Code Online (Sandbox Code Playgroud)
这是我用来将枚举值作为字符串的扩展方法.首先是枚举.
public enum DatabaseEnvironment
{
[Description("AzamSharpBlogDevDatabase")]
Development = 1,
[Description("AzamSharpBlogQADatabase")]
QualityAssurance = 2,
[Description("AzamSharpBlogTestDatabase")]
Test = 3
}
Run Code Online (Sandbox Code Playgroud)
Description属性来自System.ComponentModel.
这是我的扩展方法:
public static string GetValueAsString(this DatabaseEnvironment environment)
{
// get the field
var field = environment.GetType().GetField(environment.ToString());
var customAttributes = field.GetCustomAttributes(typeof (DescriptionAttribute), false);
if(customAttributes.Length > 0)
{
return (customAttributes[0] as DescriptionAttribute).Description;
}
else
{
return environment.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以使用以下代码访问枚举作为字符串值:
[TestFixture]
public class when_getting_value_of_enum
{
[Test]
public void should_get_the_value_as_string()
{
Assert.AreEqual("AzamSharpBlogTestDatabase",DatabaseEnvironment.Test.GetValueAsString());
}
}
Run Code Online (Sandbox Code Playgroud)
处理问题的另一种方法是使用枚举和字符串数组,将枚举值与字符串列表进行映射:
public enum GroupTypes
{
TheGroup = 0,
TheOtherGroup
}
string[] GroupTypesStr = {
"OEM",
"CMB"
};
Run Code Online (Sandbox Code Playgroud)
你可以使用这样的东西:
Log.Write(GroupTypesStr[(int)GroupTypes.TheOtherGroup]);
Run Code Online (Sandbox Code Playgroud)
它会提示CMB
优点:
缺点:
为什么不直接使用相同的枚举,而只是调用.ToString()?
using System;
public class EnumSample
{
enum Holidays
{
Christmas = 1,
Easter = 2
};
public static void Main()
{
Enum myHolidays = Holidays.Christmas;
Console.WriteLine("The value of this instance is '{0}'", myHolidays.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
您是否考虑过使用字典的查找表?
enum GroupTypes
{
TheGroup,
TheOtherGroup
}
Dictionary<string, GroupTypes> GroupTypeLookup = new Dictionary<string, GroupTypes>();
// initialize lookup table:
GroupTypeLookup.Add("OEM", TheGroup);
GroupTypeLookup.Add("CMB", TheOtherGroup);
Run Code Online (Sandbox Code Playgroud)
然后,您可以在读取字符串时使用 GroupTypeLookup.TryGetValue() 来查找字符串。