比较两个类型为T的System.Enum [英] Compare two System.Enum of type T

查看:130
本文介绍了比较两个类型为T的System.Enum的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我非常了解泛型(我认为)。
然而,只是认为System.Enum不易作为泛型类型实现。
我有这个类:

  public class Button< TEnum> TEnum:struct,IConvertible,IComparable,IFormattable {
public TEnum Identifier {
get;
私人套餐; //设置在ctor
}
}

  public abstract class AbstractInputDevice< TEnum> TEnum:struct,IConvertible,IComparable,IFormattable {

private List< Button< TEnum>> _buttons = new List< Button< TEnum>>();

公共按钮< TEnum> GetButton(TEnum Identifier){
foreach(_buttons中的Button< TEnum>按钮){
if(button.Identifier == Identifier)//< - 编译器抛出
return button;
}
Debug.Log('+ GetType()。Name +'不能返回< b>未注册的< / b>'+ typeof(Button< TEnum> ''听''+ typeof(TEnum).Name +。+ Identifier.ToString()+'。);
返回null;


InputDevice可能如下所示:

  public class Keyboard:AbstractInputDevice< KeyCode> {
private void Useless(){
Button< KeyCode> = GetButton(KeyCode.A);




$ b $ p
$ b

编译器在这里引发一个编译错误: p>

  if(button.Identifier == Identifier)//在AbstractInputDevice上面

我相信我无法比较这两个TEnums,因为它们实际上并不是Enums。

因此没有比较方法可用。



我使用此资源:

创建泛型方法约束T到一个枚举



我赞赏任何更好的解决方案或修复。

(但我想保留Enum条目作为参数 GetButton(EnumEntry) $ div




$ $ p> button.Identifier ==标识符

您应该使用

  EqualityComparer< TEnum> .Default.Equals(button.Identifier,Identifier)

这可避免将值装箱到对象框(或 IComparable 框)。


I am pretty close to understand Generics now (I think).
However, just figured that System.Enum is not easy to implement as a generic type. I have this class:

public class Button<TEnum> where TEnum : struct, IConvertible, IComparable, IFormattable {
   public TEnum Identifier {
        get;
        private set; //Set in the ctor
    }
}

and

public abstract class AbstractInputDevice<TEnum> where TEnum : struct, IConvertible, IComparable, IFormattable {

   private List<Button<TEnum>> _buttons = new List<Button<TEnum>>();

   public Button<TEnum> GetButton(TEnum Identifier){
        foreach(Button<TEnum> button in _buttons){
            if(button.Identifier == Identifier) //<- compiler throws
                return button;
        }
        Debug.Log("'" + GetType().Name + "' cannot return an <b>unregistered</b> '" + typeof(Button<TEnum>).Name + "' that listens to '" + typeof(TEnum).Name + "." + Identifier.ToString() + "'.");
        return null;
    }
}

An InputDevice might look like that:

public class Keyboard : AbstractInputDevice<KeyCode> {
    private void Useless(){
        Button<KeyCode> = GetButton(KeyCode.A);
    }
}

The compiler throws a compile error right here:

if(button.Identifier == Identifier) //In AbstractInputDevice above

I believe I cannot compare these two TEnums because they are not actually known to be Enums.
And thus no comparison method is available.

I used this resource:
Create Generic method constraining T to an Enum

I appreciate any better solution or fix.
(But I want to keep the Enum entry as a parameter to GetButton(EnumEntry))

解决方案

Instead of the impossible

button.Identifier == Identifier

you should use

EqualityComparer<TEnum>.Default.Equals(button.Identifier, Identifier)

This avoids boxing the value into an object box (or IComparable box).

这篇关于比较两个类型为T的System.Enum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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