C#,旗枚举,泛型函数来寻找一个标志 [英] C#, Flags Enum, Generic function to look for a flag

查看:212
本文介绍了C#,旗枚举,泛型函数来寻找一个标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,可以与任何风格的标志枚举用于查看是否存在标志一个一般用途的功能。



这不能编译,但如果。任何人有一个建议,我会很感激的。

 公共静态布尔IsEnumFlagPresent< T>(吨价,T lookingForFlag)
,其中T:枚举
{
布尔结果=((价值&安培; lookingForFlag)== lookingForFlag);
返回结果;
}


解决方案

没有,你不能用C#泛型做到这一点。然而,你的可能的事:

 公共静态布尔IsEnumFlagPresent< T>(吨价,T lookingForFlag )
,其中T:结构
{
INT的intValue =(int)的(对象)的价值;
INT intLookingForFlag =(int)的(对象)lookingForFlag;
回报率((&的intValue安培; intLookingForFlag)== intLookingForFlag);
}

这将仅适用于有基础类型<$ C $的枚举工作C> INT ,它是有点低效,因为它框的值...但它应该工作。



您可能需要添加一个执行键入检查那件T实际上是一个枚举类型(如的typeof(T).BaseType == typeof运算(枚举)



下面是一个完整的程序演示它的工作:

 使用系统; 

[国旗]
枚举符
{
A = 1,
B = 2,
C = 4,
D = 8
}

类测试
{
公共静态布尔IsEnumFlagPresent< T>(吨价,T lookingForFlag)
,其中T:结构
{
INT的intValue =(int)的(对象)的价值;
INT intLookingForFlag =(int)的(对象)lookingForFlag;
回报率((&的intValue安培; intLookingForFlag)== intLookingForFlag);
}

静态无效的主要()
{
Console.WriteLine(IsEnumFlagPresent(Foo.B | foo.c的,Foo.A));
Console.WriteLine(IsEnumFlagPresent(Foo.B | foo.c的,Foo.B));
Console.WriteLine(IsEnumFlagPresent(Foo.B | foo.c的,foo.c中));
Console.WriteLine(IsEnumFlagPresent(Foo.B | foo.c的,Foo.D));
}
}


I'd like one general purpose function that could be used with any Flags style enum to see if a flag exists.

This doesn't compile, but if anyone has a suggestion, I'd appreciate it.

public static Boolean IsEnumFlagPresent<T>(T value,T lookingForFlag) 
       where T:enum
{
    Boolean result = ((value & lookingForFlag) == lookingForFlag);
    return result ;            
}

解决方案

No, you can't do this with C# generics. However, you could do:

public static bool IsEnumFlagPresent<T>(T value, T lookingForFlag) 
    where T : struct
{
    int intValue = (int) (object) value;
    int intLookingForFlag = (int) (object) lookingForFlag;
    return ((intValue & intLookingForFlag) == intLookingForFlag);
}

This will only work for enums which have an underlying type of int, and it's somewhat inefficient because it boxes the value... but it should work.

You may want to add an execution type check that T is actually an enum type (e.g. typeof(T).BaseType == typeof(Enum))

Here's a complete program demonstrating it working:

using System;

[Flags]
enum Foo
{
    A = 1,
    B = 2,
    C = 4,
    D = 8
}

class Test
{
    public static Boolean IsEnumFlagPresent<T>(T value, T lookingForFlag) 
        where T : struct
    {
        int intValue = (int) (object) value;
        int intLookingForFlag = (int) (object) lookingForFlag;
        return ((intValue & intLookingForFlag) == intLookingForFlag);
    }

    static void Main()
    {
        Console.WriteLine(IsEnumFlagPresent(Foo.B | Foo.C, Foo.A));
        Console.WriteLine(IsEnumFlagPresent(Foo.B | Foo.C, Foo.B));
        Console.WriteLine(IsEnumFlagPresent(Foo.B | Foo.C, Foo.C));
        Console.WriteLine(IsEnumFlagPresent(Foo.B | Foo.C, Foo.D));
    }
}

这篇关于C#,旗枚举,泛型函数来寻找一个标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆