c# - 変換 - enum キャスト c言語
C#で列挙型にint型にキャスト (17)
4.0 .NET Frameworkを使用する準備が整ったら、非常に便利で新しい[Flags]属性を使用する新しい Enum.TryParse() 関数があります。 Enum.TryParseメソッド(文字列、TEnum%)を 参照してください。
int
をC#の
enum
にキャストするにはどうすればよいですか。
intをc#のenumにキャストするための簡単で明確な方法:
public class Program
{
public enum Color : int
{
Blue = 0,
Black = 1,
Green = 2,
Gray = 3,
Yellow =4
}
public static void Main(string[] args)
{
//from string
Console.WriteLine((Color) Enum.Parse(typeof(Color), "Green"));
//from int
Console.WriteLine((Color)2);
//From number you can also
Console.WriteLine((Color)Enum.ToObject(typeof(Color) ,2));
}
}
このコードを使用して、intを自分の列挙型にキャストします。
if (typeof(YourEnum).IsEnumDefined(valueToCast)) return (YourEnum)valueToCast;
else { //handle it here, if its not defined }
私はそれが最善の解決策だと思います。
これは
Int32
を
Enum
キャストする拡張メソッドです。
値が可能な最大値よりも大きい場合でも、ビット単位のフラグが適用されます。 たとえば、 1、2 、および 4の 可能性を持つ列挙型があり、intが 9の 場合、 8 がない場合は 1 と解釈されます。 これにより、コードの更新よりも先にデータを更新できます。
public static TEnum ToEnum<TEnum>(this int val) where TEnum : struct, IComparable, IFormattable, IConvertible
{
if (!typeof(TEnum).IsEnum)
{
return default(TEnum);
}
if (Enum.IsDefined(typeof(TEnum), val))
{//if a straightforward single value, return that
return (TEnum)Enum.ToObject(typeof(TEnum), val);
}
var candidates = Enum
.GetValues(typeof(TEnum))
.Cast<int>()
.ToList();
var isBitwise = candidates
.Select((n, i) => {
if (i < 2) return n == 0 || n == 1;
return n / 2 == candidates[i - 1];
})
.All(y => y);
var maxPossible = candidates.Sum();
if (
Enum.TryParse(val.ToString(), out TEnum asEnum)
&& (val <= maxPossible || !isBitwise)
){//if it can be parsed as a bitwise enum with multiple flags,
//or is not bitwise, return the result of TryParse
return asEnum;
}
//If the value is higher than all possible combinations,
//remove the high imaginary values not accounted for in the enum
var excess = Enumerable
.Range(0, 32)
.Select(n => (int)Math.Pow(2, n))
.Where(n => n <= val && n > 0 && !candidates.Contains(n))
.Sum();
return Enum.TryParse((val - excess).ToString(), out asEnum) ? asEnum : default(TEnum);
}
これは、上記のTawaniのユーティリティクラスのような総称を使用して、dot.NET 4.0で部分一致を使用して整数または文字列をターゲットのenumに解析します。 私はそれが不完全かもしれないコマンドラインスイッチ変数を変換するためにそれを使っています。 列挙型をnullにすることはできないため、デフォルト値を論理的に指定する必要があります。 これは次のように呼ぶことができます。
var result = EnumParser<MyEnum>.Parse(valueToParse, MyEnum.FirstValue);
これがコードです:
using System;
public class EnumParser<T> where T : struct
{
public static T Parse(int toParse, T defaultVal)
{
return Parse(toParse + "", defaultVal);
}
public static T Parse(string toParse, T defaultVal)
{
T enumVal = defaultVal;
if (defaultVal is Enum && !String.IsNullOrEmpty(toParse))
{
int index;
if (int.TryParse(toParse, out index))
{
Enum.TryParse(index + "", out enumVal);
}
else
{
if (!Enum.TryParse<T>(toParse + "", true, out enumVal))
{
MatchPartialName(toParse, ref enumVal);
}
}
}
return enumVal;
}
public static void MatchPartialName(string toParse, ref T enumVal)
{
foreach (string member in enumVal.GetType().GetEnumNames())
{
if (member.ToLower().Contains(toParse.ToLower()))
{
if (Enum.TryParse<T>(member + "", out enumVal))
{
break;
}
}
}
}
}
FYI: 質問は整数についてでした、言及された誰もまた明示的にEnum.TryParse()に変換しません
それはあなたがユーザーが望む 列挙型に 任意の入力データを変換するのを助けることができます。 以下のようなenumがあるとします。デフォルトでは int です。 列挙型の最初に Default 値を追加してください。 入力値と一致するものが見つからない場合に、ヘルパーmedthodで使用されます。
public enum FriendType
{
Default,
Audio,
Video,
Image
}
public static class EnumHelper<T>
{
public static T ConvertToEnum(dynamic value)
{
var result = default(T);
var tempType = 0;
//see Note below
if (value != null &&
int.TryParse(value.ToString(), out tempType) &&
Enum.IsDefined(typeof(T), tempType))
{
result = (T)Enum.ToObject(typeof(T), tempType);
}
return result;
}
}
注意: enumはデフォルトで int であるため、ここでは値をintに解析してみます。enumをこのように定義した場合、これは バイト 型です。
public enum MediaType : byte
{
Default,
Audio,
Video,
Image
}
あなたはからヘルパーメソッドで解析を変更する必要があります
int.TryParse(value.ToString(), out tempType)
に
byte.TryParse(value.ToString(), out tempType)
以下の入力について私の方法をチェックします
EnumHelper<FriendType>.ConvertToEnum(null);
EnumHelper<FriendType>.ConvertToEnum("");
EnumHelper<FriendType>.ConvertToEnum("-1");
EnumHelper<FriendType>.ConvertToEnum("6");
EnumHelper<FriendType>.ConvertToEnum("");
EnumHelper<FriendType>.ConvertToEnum("2");
EnumHelper<FriendType>.ConvertToEnum(-1);
EnumHelper<FriendType>.ConvertToEnum(0);
EnumHelper<FriendType>.ConvertToEnum(1);
EnumHelper<FriendType>.ConvertToEnum(9);
私の英語を残念に思う
より堅牢であるためには、いくつかの型のマッチング緩和を組み込むべきです。
public static T ToEnum<T>(dynamic value)
{
if (value == null)
{
// default value of an enum is the object that corresponds to
// the default value of its underlying type
// https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/default-values-table
value = Activator.CreateInstance(Enum.GetUnderlyingType(typeof(T)));
}
else if (value is string name)
{
return (T)Enum.Parse(typeof(T), name);
}
return (T)Enum.ToObject(typeof(T),
Convert.ChangeType(value, Enum.GetUnderlyingType(typeof(T))));
}
テストケース
[Flags]
public enum A : uint
{
None = 0
X = 1 < 0,
Y = 1 < 1
}
static void Main(string[] args)
{
var value = EnumHelper.ToEnum<A>(7m);
var x = value.HasFlag(A.X); // true
var y = value.HasFlag(A.Y); // true
var value2 = EnumHelper.ToEnum<A>("X");
var value3 = EnumHelper.ToEnum<A>(null);
Console.ReadKey();
}
ビットマスクとして機能し、[Flags]列挙内の1つ以上の値を表すことができる整数がある場合は、このコードを使用して個々のフラグ値をリストに解析できます。
for (var flagIterator = 0; flagIterator < 32; flagIterator++)
{
// Determine the bit value (1,2,4,...,Int32.MinValue)
int bitValue = 1 << flagIterator;
// Check to see if the current flag exists in the bit mask
if ((intValue & bitValue) != 0)
{
// If the current flag exists in the enumeration, then we can add that value to the list
// if the enumeration has that flag defined
if (Enum.IsDefined(typeof(MyEnum), bitValue))
Console.WriteLine((MyEnum)bitValue);
}
}
これは、
enum
型の基本型が符号付き32ビット整数であると仮定していることに注意してください。
それが異なる数値型だった場合は、その型のビットを反映するようにハードコードされた32を変更する必要があります(または
Enum.GetUnderlyingType()
を使用してプログラムで派生させます)。
以下はやや良い拡張方法です
public static string ToEnumString<TEnum>(this int enumValue)
{
var enumString = enumValue.ToString();
if (Enum.IsDefined(typeof(TEnum), enumValue))
{
enumString = ((TEnum) Enum.ToObject(typeof (TEnum), enumValue)).ToString();
}
return enumString;
}
元の質問から少し離れまし
たが、の質問に対する回答
を見つけまし
た
。enumからint値を取得すると
便利です。
public const int
プロパティを使用して静的クラスを作成すると、関連する
int
定数を簡単にまとめて使用できるようになります。これらを使用するときに
int
にキャストする必要はありません。
public static class Question
{
public static readonly int Role = 2;
public static readonly int ProjectFunding = 3;
public static readonly int TotalEmployee = 4;
public static readonly int NumberOfServers = 5;
public static readonly int TopBusinessConcern = 6;
}
明らかに、enum型の機能のいくつかは失われるでしょうが、たくさんのデータベースID定数を保存するためには、それはかなりきちんとした解決策のようです。
数値の場合、何をしてもオブジェクトを返すので安全です。
public static class EnumEx
{
static public bool TryConvert<T>(int value, out T result)
{
result = default(T);
bool success = Enum.IsDefined(typeof(T), value);
if (success)
{
result = (T)Enum.ToObject(typeof(T), value);
}
return success;
}
}
文字列から:
YourEnum foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourString);
// the foo.ToString().Contains(",") check is necessary for enumerations marked with an [Flags] attribute
if (!Enum.IsDefined(typeof(YourEnum), foo) && !foo.ToString().Contains(","))
throw new InvalidOperationException($"{yourString} is not an underlying value of the YourEnum enumeration.")
intから:
YourEnum foo = (YourEnum)yourInt;
更新:
番号からもできます
YourEnum foo = (YourEnum)Enum.ToObject(typeof(YourEnum) , yourInt);
次の例を見てください。
int one = 1;
MyEnum e = (MyEnum)one;
私の場合は、WCFサービスから列挙型を返す必要がありました。 enum.ToString()だけでなく、わかりやすい名前も必要でした。
これが私のWCFクラスです。
[DataContract]
public class EnumMember
{
[DataMember]
public string Description { get; set; }
[DataMember]
public int Value { get; set; }
public static List<EnumMember> ConvertToList<T>()
{
Type type = typeof(T);
if (!type.IsEnum)
{
throw new ArgumentException("T must be of type enumeration.");
}
var members = new List<EnumMember>();
foreach (string item in System.Enum.GetNames(type))
{
var enumType = System.Enum.Parse(type, item);
members.Add(
new EnumMember() { Description = enumType.GetDescriptionValue(), Value = ((IConvertible)enumType).ToInt32(null) });
}
return members;
}
}
これは、EnumからDescriptionを取得するExtensionメソッドです。
public static string GetDescriptionValue<T>(this T source)
{
FieldInfo fileInfo = source.GetType().GetField(source.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fileInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
{
return attributes[0].Description;
}
else
{
return source.ToString();
}
}
実装:
return EnumMember.ConvertToList<YourType>();
私は完全な答えを得ると思います、人々は列挙型が.NETでどのように内部的に働くかを知っていなければなりません。
仕組み
.NETの列挙型は、一連の値(フィールド)を基本型(デフォルトは
int
)にマップする構造です。
しかし、実際にはあなたのenumがマップする整数型を選択することができます。
public enum Foo : short
この場合、enumは
short
データ型にマップされます。
short
、それはshortとしてメモリに格納され、キャストして使用するとshortとして動作します。
ILの観点から見ると、(normal、int)enumは次のようになります。
.class public auto ansi serializable sealed BarFlag extends System.Enum
{
.custom instance void System.FlagsAttribute::.ctor()
.custom instance void ComVisibleAttribute::.ctor(bool) = { bool(true) }
.field public static literal valuetype BarFlag AllFlags = int32(0x3fff)
.field public static literal valuetype BarFlag Foo1 = int32(1)
.field public static literal valuetype BarFlag Foo2 = int32(0x2000)
// and so on for all flags or enum values
.field public specialname rtspecialname int32 value__
}
ここで注意しなければならないのは、
value__
はenum値とは別に格納されているということです。
上記の列挙型
Foo
場合、
value__
の型はint16です。
これは基本的に
、型が一致する限り
、必要なものはすべて列挙
型に
格納できることを意味します。
この時点で、
System.Enum
は値型であることを指摘したいと思います。これは基本的に、
BarFlag
はメモリ内で4バイトを
BarFlag
、
Foo
は2を
BarFlag
することを意味します。それよりも複雑ですが、ちょっと...)。
答え
そのため、もしあなたがenumにマップしたい整数を持っていれば、ランタイムは2つのことをするだけです:4バイトをコピーし、それに何か他のもの(enumの名前)をつける。 データは値型として格納されるため、コピーは暗黙的に行われます。つまり、管理されていないコードを使用する場合は、データをコピーせずに単純に列挙型と整数型を交換できます。
安全を期すために、 基礎となる型が同じか暗黙的に変換可能 であることを 知っている ことと、enum値が存在することを保証することがベストプラクティスだと思います(デフォルトではチェックされません)。
これがどのように機能するかを確認するには、次のコードを試してください。
public enum MyEnum : int
{
Foo = 1,
Bar = 2,
Mek = 5
}
static void Main(string[] args)
{
var e1 = (MyEnum)5;
var e2 = (MyEnum)6;
Console.WriteLine("{0} {1}", e1, e2);
Console.ReadLine();
}
e2
へのキャストも機能します。
上記のコンパイラの観点からすると、これは理にかなっています
value__
フィールドは単に5または6で埋められ、
Console.WriteLine
が
ToString()
呼び出すと、
e1
の名前は解決されますが、
e2
の名前は解決されません。
それが意図したものではない場合は、
Enum.IsDefined(typeof(MyEnum), 6)
を使用して、キャストしている値が定義済みのenumにマップされているかどうかを確認してください。
また、コンパイラが実際にこれをチェックしているにもかかわらず、列挙型の基本型については明示的に説明しています。 私はこれをやっているのですが、道に迷っても驚かないようにしています。 これらの驚くべき動作を確認するには、次のコードを使用できます(実際には、これはデータベースコードでよく発生することがわかりました)。
public enum MyEnum : short
{
Mek = 5
}
static void Main(string[] args)
{
var e1 = (MyEnum)32769; // will not compile, out of bounds for a short
object o = 5;
var e2 = (MyEnum)o; // will throw at runtime, because o is of type int
Console.WriteLine("{0} {1}", e1, e2);
Console.ReadLine();
}
文字列をENUMに、またはintをENUM定数に変換するには、Enum.Parse関数を使用する必要があります。 これはYouTubeのビデオ https://www.youtube.com/watch?v=4nhx4VwdRDk これは実際には文字列で示されており、intについても同様です。
コードは以下のようになります。ここで、 "red"は文字列、 "MyColors"は色定数を持つ色ENUMです。
MyColors EnumColors = (MyColors)Enum.Parse(typeof(MyColors), "Red");
MyEnum
型のオブジェクトがある場合もあります。
好き
var MyEnumType = typeof(MyEnumType);
その後:
Enum.ToObject(typeof(MyEnum), 3)