C# 检查一个枚举的值是否在另一个枚举中

2022年10月13日 1243点热度 0人点赞 0条评论
内容目录
 bool IsDefine<T1, T2>(T2 t)
  where T1 : Enum
  where T2 : Enum
{
    var value = Unsafe.As<T2, T1>(ref t);
    var array = Enum.GetValues(typeof(T1));
    return (Array.BinarySearch(array, value) >= 0);
}
        public static class EnumTool<T1, T2>
         where T1 : Enum
         where T2 : Enum
        {
            private static TCache<T1> _cache;
            private class TCache<T>
            {
                public TCache(Array array)
                {
                    Array = array;
                }
                public Array Array { get; private set; }
            }

            static EnumTool()
            {
                _cache = new TCache<T1>(Enum.GetValues(typeof(T1)));
            }
            public static bool IsDefine(T2 t)
            {
                var value = Unsafe.As<T2, T1>(ref t);
                return (Array.BinarySearch(_cache.Array, value) >= 0);
            }
        }
public static class EnumTool<T1, T2>
 where T1 : Enum
 where T2 : Enum
{
    private static TCache<T1> _cache;
    private class TCache<T>
    {
        public TCache(int[] array)
        {
            Array = array;
        }
        public int[] Array { get; private set; }
    }

    static EnumTool()
    {
        var values = Enum.GetValues(typeof(T1));
        int[] array = new int[values.Length];
        for (int i = 0; i < values.Length; i++)
        {
            array[i] = (int)values.GetValue(i)!;
        }
        _cache = new TCache<T1>(array);
    }
    public static bool IsDefine(T2 t)
    {
        var value = Unsafe.As<T2, T1>(ref t);
        return (Array.BinarySearch(_cache.Array, value) >= 0);
    }
}
    public SelectedItem[] s = new SelectedItem[]
    {
        new SelectedItem("1","1"),
        new SelectedItem("2","2")
    };
    private PrintModal<导出模板管理>? printModal;

    private Task OpenPrint()
    {
        return printModal!.OpenDialog();
    }
}

痴者工良

高级程序员劝退师

文章评论