enum ジェネリック制約がない場合の良い回避策を知っている人はいますか?
質問
私がやりたいことは次のようなものです:フラグ付きの値を組み合わせた列挙型があります。
public static class EnumExtension
{
public static bool IsSet<T>( this T input, T matchTo )
where T:enum //the constraint I want that doesn't exist in C#3
{
return (input & matchTo) != 0;
}
}
それで、私は次のようにすることができます:
MyEnum tester = MyEnum.FlagA | MyEnum.FlagB
if( tester.IsSet( MyEnum.FlagA ) )
//act on flag a
残念ながら、C# のジェネリック where 制約には列挙型の制限がなく、クラスと構造体のみがあります。C# は列挙型を (値型であっても) 構造体として認識しないため、このような拡張型を追加することはできません。
誰か回避策を知っていますか?
解決
編集:これは現在、UnconstrainedMelody のバージョン 0.0.0.2 で有効です。
(私のリクエストに応じて、 enum 制約に関するブログ投稿. 。独立した回答として、以下に基本的な事実を含めました。)
最善の解決策は、私がそれを含めるのを待つことです アンコンストレイントメロディー1. 。これは、次のような「偽の」制約を持つ C# コードを使用するライブラリです。
where T : struct, IEnumConstraint
そしてそれをに変えます
where T : struct, System.Enum
ポストビルドステップ経由。
書くのはそれほど難しくないはずです IsSet
...両方に対応していますが Int64
-ベースおよび UInt64
-ベースのフラグは注意が必要な部分かもしれません。(ヘルパー メソッドがいくつか登場しているように感じます。基本的に、フラグ列挙型を基本型の UInt64
.)
電話をかけたらどのような動作をしてもらいたいですか
tester.IsSet(MyFlags.A | MyFlags.C)
?それを確認すべきか 全て 指定されたフラグは設定されていますか?それが私の期待です。
今夜の帰りにやってみようと思います...便利な enum メソッドをざっと取り上げて、ライブラリをすぐに使用可能な標準に引き上げてから、少しリラックスしたいと考えています。
編集:よくわかりません IsSet
ちなみに名前としては。オプション:
- 含まれるもの
- 含まれています
- HasFlag (または HasFlags)
- IsSet (確かにオプションです)
ご意見は大歓迎です。いずれにせよ、何かが固まるまでにはしばらく時間がかかると思います...
1 もちろんパッチとして提出することもできます...
他のヒント
Darren、型が特定の列挙型であれば機能するでしょう。一般的な列挙型が機能するには、それらを int (または、おそらく uint) にキャストしてブール計算を行う必要があります。
public static bool IsSet( this Enum input, Enum matchTo )
{
return ( Convert.ToUInt32( input ) & Convert.ToUInt32( matchTo ) ) != 0;
}
C# 7.3 では、列挙型制約を追加する方法が組み込まれています。
public class UsingEnum<T> where T : System.Enum { }
実はそれは、醜いトリックを使えば可能です。ただし、拡張メソッドには使用できません。
public abstract class Enums<Temp> where Temp : class {
public static TEnum Parse<TEnum>(string name) where TEnum : struct, Temp {
return (TEnum)Enum.Parse(typeof(TEnum), name);
}
}
public abstract class Enums : Enums<Enum> { }
Enums.IsSet<DateTimeKind>("Local")
望むなら与えてもいいよ Enums<Temp>
プライベート コンストラクターとパブリックのネストされた抽象継承クラス Temp
として Enum
, 、非列挙型のバージョンが継承されないようにします。
これは、IL ウィービングを使用して実現できます。 追加の制約
このコードを記述できるようにします
public class Sample
{
public void MethodWithDelegateConstraint<[DelegateConstraint] T> ()
{
}
public void MethodWithEnumConstraint<[EnumConstraint] T>()
{
}
}
コンパイルされる内容
public class Sample
{
public void MethodWithDelegateConstraint<T>() where T: Delegate
{
}
public void MethodWithEnumConstraint<T>() where T: struct, Enum
{
}
}
これは元の質問の答えにはなりませんが、.NET 4 には次のメソッドがあります。 Enum.HasFlag これはあなたの例でやろうとしていることを行います
私がそれを行う方法は、構造体制約を設定し、実行時に T が列挙型であることを確認することです。これで問題が完全に解消されるわけではありませんが、多少は軽減されます
C# 7.3 以降では、ジェネリック型に対して Enum 制約を使用できます。
public static TEnum Parse<TEnum>(string value) where TEnum : Enum
{
return (TEnum) Enum.Parse(typeof(TEnum), value);
}
Nullable enum を使用する場合は、元の構造体制約をそのままにしておく必要があります。
public static TEnum? TryParse<TEnum>(string value) where TEnum : struct, Enum
{
if( Enum.TryParse(value, out TEnum res) )
return res;
else
return null;
}
元のコードを使用すると、メソッド内でリフレクションを使用して T が列挙型であるかどうかをテストすることもできます。
public static class EnumExtension
{
public static bool IsSet<T>( this T input, T matchTo )
{
if (!typeof(T).IsEnum)
{
throw new ArgumentException("Must be an enum", "input");
}
return (input & matchTo) != 0;
}
}
以下に私が作成したコードをいくつか示します。これは、あまりおかしなことをしなくても、希望通りに動作すると思われます。フラグとして設定された列挙型のみに限定されるわけではありませんが、必要に応じて常にチェックを入れることができます。
public static class EnumExtensions
{
public static bool ContainsFlag(this Enum source, Enum flag)
{
var sourceValue = ToUInt64(source);
var flagValue = ToUInt64(flag);
return (sourceValue & flagValue) == flagValue;
}
public static bool ContainsAnyFlag(this Enum source, params Enum[] flags)
{
var sourceValue = ToUInt64(source);
foreach (var flag in flags)
{
var flagValue = ToUInt64(flag);
if ((sourceValue & flagValue) == flagValue)
{
return true;
}
}
return false;
}
// found in the Enum class as an internal method
private static ulong ToUInt64(object value)
{
switch (Convert.GetTypeCode(value))
{
case TypeCode.SByte:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
return (ulong)Convert.ToInt64(value, CultureInfo.InvariantCulture);
case TypeCode.Byte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
return Convert.ToUInt64(value, CultureInfo.InvariantCulture);
}
throw new InvalidOperationException("Unknown enum type.");
}
}
誰かが汎用 IsSet (すぐにすぐに作成できるので改善できる)、または文字列から Enum へのオンフライ変換 (以下に示す EnumConstraint を使用) を必要とする場合:
public class TestClass
{ }
public struct TestStruct
{ }
public enum TestEnum
{
e1,
e2,
e3
}
public static class TestEnumConstraintExtenssion
{
public static bool IsSet<TEnum>(this TEnum _this, TEnum flag)
where TEnum : struct
{
return (((uint)Convert.ChangeType(_this, typeof(uint))) & ((uint)Convert.ChangeType(flag, typeof(uint)))) == ((uint)Convert.ChangeType(flag, typeof(uint)));
}
//public static TestClass ToTestClass(this string _this)
//{
// // #generates compile error (so no missuse)
// return EnumConstraint.TryParse<TestClass>(_this);
//}
//public static TestStruct ToTestStruct(this string _this)
//{
// // #generates compile error (so no missuse)
// return EnumConstraint.TryParse<TestStruct>(_this);
//}
public static TestEnum ToTestEnum(this string _this)
{
// #enum type works just fine (coding constraint to Enum type)
return EnumConstraint.TryParse<TestEnum>(_this);
}
public static void TestAll()
{
TestEnum t1 = "e3".ToTestEnum();
TestEnum t2 = "e2".ToTestEnum();
TestEnum t3 = "non existing".ToTestEnum(); // default(TestEnum) for non existing
bool b1 = t3.IsSet(TestEnum.e1); // you can ommit type
bool b2 = t3.IsSet<TestEnum>(TestEnum.e2); // you can specify explicite type
TestStruct t;
// #generates compile error (so no missuse)
//bool b3 = t.IsSet<TestEnum>(TestEnum.e1);
}
}
Enum コーディング制約を作成するためにサンプル ホットがまだ必要な場合は、次のようにします。
using System;
/// <summary>
/// would be same as EnumConstraint_T<Enum>Parse<EnumType>("Normal"),
/// but writen like this it abuses constrain inheritence on System.Enum.
/// </summary>
public class EnumConstraint : EnumConstraint_T<Enum>
{
}
/// <summary>
/// provides ability to constrain TEnum to System.Enum abusing constrain inheritence
/// </summary>
/// <typeparam name="TClass">should be System.Enum</typeparam>
public abstract class EnumConstraint_T<TClass>
where TClass : class
{
public static TEnum Parse<TEnum>(string value)
where TEnum : TClass
{
return (TEnum)Enum.Parse(typeof(TEnum), value);
}
public static bool TryParse<TEnum>(string value, out TEnum evalue)
where TEnum : struct, TClass // struct is required to ignore non nullable type error
{
evalue = default(TEnum);
return Enum.TryParse<TEnum>(value, out evalue);
}
public static TEnum TryParse<TEnum>(string value, TEnum defaultValue = default(TEnum))
where TEnum : struct, TClass // struct is required to ignore non nullable type error
{
Enum.TryParse<TEnum>(value, out defaultValue);
return defaultValue;
}
public static TEnum Parse<TEnum>(string value, TEnum defaultValue = default(TEnum))
where TEnum : struct, TClass // struct is required to ignore non nullable type error
{
TEnum result;
if (Enum.TryParse<TEnum>(value, out result))
return result;
return defaultValue;
}
public static TEnum Parse<TEnum>(ushort value)
{
return (TEnum)(object)value;
}
public static sbyte to_i1<TEnum>(TEnum value)
{
return (sbyte)(object)Convert.ChangeType(value, typeof(sbyte));
}
public static byte to_u1<TEnum>(TEnum value)
{
return (byte)(object)Convert.ChangeType(value, typeof(byte));
}
public static short to_i2<TEnum>(TEnum value)
{
return (short)(object)Convert.ChangeType(value, typeof(short));
}
public static ushort to_u2<TEnum>(TEnum value)
{
return (ushort)(object)Convert.ChangeType(value, typeof(ushort));
}
public static int to_i4<TEnum>(TEnum value)
{
return (int)(object)Convert.ChangeType(value, typeof(int));
}
public static uint to_u4<TEnum>(TEnum value)
{
return (uint)(object)Convert.ChangeType(value, typeof(uint));
}
}
これが誰かの役に立つことを願っています。
Enum を一般的な制約として追加したかっただけです。
これは、を使用した小さなヘルパー メソッドにすぎませんが、 ExtraConstraints
私にとっては少しオーバーヘッドが大きすぎます。
ただ作成することにしました struct
制約を追加し、実行時チェックを追加します。 IsEnum
. 。変数を T から Enum に変換するには、まずオブジェクトにキャストします。
public static Converter<T, string> CreateConverter<T>() where T : struct
{
if (!typeof(T).IsEnum) throw new ArgumentException("Given Type is not an Enum");
return new Converter<T, string>(x => ((Enum)(object)x).GetEnumDescription());
}