列挙型の値を人間が判読可能な値に変換する方法を知っている人はいますか?例えば列挙型を人間が判読可能な値に変換する
:
ThisIsValueAは "これが値Aである" でなければなりません。私が見てきたこれの
列挙型の値を人間が判読可能な値に変換する方法を知っている人はいますか?例えば列挙型を人間が判読可能な値に変換する
:
ThisIsValueAは "これが値Aである" でなければなりません。私が見てきたこれの
VBのコードからこれを変換する私は以来、首尾よく生産でこれを使用していた...特定のイアンHorwillがblog post long agoに残っていることスニペット。
/// <summary>
/// Add spaces to separate the capitalized words in the string,
/// i.e. insert a space before each uppercase letter that is
/// either preceded by a lowercase letter or followed by a
/// lowercase letter (but not for the first char in string).
/// This keeps groups of uppercase letters - e.g. acronyms - together.
/// </summary>
/// <param name="pascalCaseString">A string in PascalCase</param>
/// <returns></returns>
public static string Wordify(string pascalCaseString)
{
Regex r = new Regex("(?<=[a-z])(?<x>[A-Z])|(?<=.)(?<x>[A-Z])(?=[a-z])");
return r.Replace(pascalCaseString, " ${x}");
}
( 'System.Text.RegularExpressionsを使用して、'、必要)
したがって:
Console.WriteLine(Wordify(ThisIsValueA.ToString()));
が戻ってくる、
"This Is Value A".
それははるかに簡単、かつより少ない冗長ですDescription属性を提供するよりも
ここでは、インダイレクションのレイヤーを提供する必要がある場合のみ(この質問では質問しなかった)属性を使用すると便利です。
ほとんどの例では、[説明]属性を使用して列挙値をマークアップし、値と説明の間に「変換」を行うためにリフレクションを使用することを含みます。ここではそれについての古いブログ記事です:
http://geekswithblogs.net/rakker/archive/2006/05/19/78952.aspx
ます。また、この記事を見てみることができます:http://www.codeproject.com/KB/cs/enumdatabinding.aspx
これは、データバインディングについて、具体的だが、列挙型の値を飾るために属性を使用する方法を示しています属性のテキストを取得するための "GetDescription"メソッドを提供します。組み込みの記述属性を使用する際の問題は、その属性の他の使用/ユーザが存在するため、記述が望ましくないところに表示される可能性があることです。カスタム属性はその問題を解決します。
System.Reflectionの「属性」クラスから継承して、独自の「説明」クラスを作成できます。 (hereから)このように:
using System;
using System.Reflection;
namespace FunWithEnum
{
enum Coolness : byte
{
[Description("Not so cool")]
NotSoCool = 5,
Cool, // since description same as ToString no attr are used
[Description("Very cool")]
VeryCool = NotSoCool + 7,
[Description("Super cool")]
SuperCool
}
class Description : Attribute
{
public string Text;
public Description(string text)
{
Text = text;
}
}
class Program
{
static string GetDescription(Enum en)
{
Type type = en.GetType();
MemberInfo[] memInfo = type.GetMember(en.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(Description), false);
if (attrs != null && attrs.Length > 0)
return ((Description)attrs[0]).Text;
}
return en.ToString();
}
static void Main(string[] args)
{
Coolness coolType1 = Coolness.Cool;
Coolness coolType2 = Coolness.NotSoCool;
Console.WriteLine(GetDescription(coolType1));
Console.WriteLine(GetDescription(coolType2));
}
}
}
私は、アンダースコアでThisIsValueAがThis_Is_Value_Aになるようにenum値を定義することをお勧めしました。次にenumValue.toString()を実行します。
Enumsの.ToStringは、C#では比較的遅く、GetType()と似ています。名前(これはカバーの下でも使用できます)。
ソリューションが非常に高速または高性能である必要がある場合は、静的な辞書で変換をキャッシュし、そこから検索するのが最適かもしれません。
C#3を利用するための@ Leonのコードの小さな適応。これは列挙型の拡張として理にかなっています。すべてを混乱させたくない場合は、これを特定のタイプに制限することができます。
public static string Wordify(this Enum input)
{
Regex r = new Regex("(?<=[a-z])(?<x>[A-Z])|(?<=.)(?<x>[A-Z])(?=[a-z])");
return r.Replace(input.ToString() , " ${x}");
}
//then your calling syntax is down to:
MyEnum.ThisIsA.Wordify();
説明属性がSystem.ComponentModel名前空間で既に使用可能な場合は、その属性を作成する理由 –