Mos*_*ico 3 c# enums portable-class-library
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace NumberedMusicScores
{
public enum KeySignatures
{
C,
G,
D,
A,
E,
B,
FCress,
CCress,
F,
Bb,
Eb,
Ab,
Db,
Gb,
Cb
}
}
Run Code Online (Sandbox Code Playgroud)
如果我使用它,我希望FCress并CCress显示为F#和C#.怎么做到这一点?
我试过这个:怎么用?字符枚举,但Description在[Description("F#")]似乎不存在.(用红线加下划线,如果我右键单击它,它甚至不会显示任何"Resolve".
更新:澄清:
enum,而是一个配置为的类enum.我想要enum解决方案.谢谢.
PCL框架不允许该Description属性.您只需创建属性的简化版本即可.
public class MyDescription : Attribute
{
public string Description = { get; private set; }
public MyDescription(string description)
{
Description = description;
}
}
Run Code Online (Sandbox Code Playgroud)
然后用托马斯的回答这个线程,这样做:
public static string GetDescription(this Enum value)
{
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name != null)
{
FieldInfo field = type.GetField(name);
if (field != null)
{
MyDescription attr =
Attribute.GetCustomAttribute(field,
typeof(MyDescription)) as MyDescription;
if (attr != null)
{
return attr.Description;
}
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
对于你的枚举:
public enum KeySignatures
{
//...
[MyDescription("F#")]
FCress,
[MyDescription("C#")]
CCress,
//...
}
Run Code Online (Sandbox Code Playgroud)