无法比较通用值

发布于 2024-11-08 10:48:22 字数 677 浏览 0 评论 0原文

如果我运行此代码,为什么会出现以下

namespace TestCode
{
    public class EnumList<TEnum>
    {
        private IList<TEnum> _list;


        public EnumList()
        {
            _list = new List<TEnum>();
        }

        public void Add(TEnum val)
        {
            _list.Add(val);
        }

        public int Get(TEnum val)
        {
            return (int)(from one in _list
                         where one == val
                         select one).First();
        }
    }
}

错误:无法将 TEnum 类型转换为 TestCode.TEnum 类型?

(顺便说一句,这是“释义”,因为我的实际代码在家里,而我在工作)

编辑:从构造函数中删除了 ,因为这不是主要问题

How come if I run this code:

namespace TestCode
{
    public class EnumList<TEnum>
    {
        private IList<TEnum> _list;


        public EnumList()
        {
            _list = new List<TEnum>();
        }

        public void Add(TEnum val)
        {
            _list.Add(val);
        }

        public int Get(TEnum val)
        {
            return (int)(from one in _list
                         where one == val
                         select one).First();
        }
    }
}

Gives me an error to do with not being able to convert type TEnum to type TestCode.TEnum?

(This is 'paraphrased' by the way as my actual code is at home and I'm at work)

Edit: Removed the <TEnum> from the constructor as that's not the main problem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天涯离梦残月幽梦 2024-11-15 10:48:22

您应该在TEnum 类型。写“==”是什么意思?参考值等于吗? IComparable.Equals?成员之间相等吗?

You should put a constraint on the TEnum type. What do you mean when writing "=="? ReferenceEquals? IComparable.Equals? Memberwise equals?

老街孤人 2024-11-15 10:48:22

省略构造函数中的 部分,因为在这种情况下,它认为您可以在构造函数和类本身上分配不同的 TEnum。有趣的是,它并没有抱怨两者同名。

Ommit the <TEnum> part in your constructor because in this case it thinks you might be able to assign a different TEnum on the constructor and on the class itself. Funny that it does not complain about both having the same name.

眸中客 2024-11-15 10:48:22

Get() 函数有两个问题:

  1. one == val 不是有效的语句,因为 TEnum 未绑定。尝试使用 Equals(one, val) 代替(我假设 TEnum 将是枚举类型)
  2. 将结果强制转换为 int 是无效的,因为无法知道是否存在从 TEnum 到 int 的转换。

你可以尝试

public int Get(TEnum val)
{            
    var result = (from one in _list
                 where Equals(one, val)
                 select one).First();
    return (Int32) Convert.ChangeType(result, typeof(Int32));
} 

但是,这是危险的领域,并且不可能是类型安全的。

There's two problems with the Get()-function:

  1. one == val isn't a valid statement as TEnum isn't bound. Try using Equals(one, val) instead (I'm assuming that TEnum will be an enum-type)
  2. Casting the result to int is invalid as there is no way of knowing if there is a conversion from TEnum to int.

You could try

public int Get(TEnum val)
{            
    var result = (from one in _list
                 where Equals(one, val)
                 select one).First();
    return (Int32) Convert.ChangeType(result, typeof(Int32));
} 

However, this is dangerous territory and will no way be typesafe.

你穿错了嫁妆 2024-11-15 10:48:22

我会更改 Get 方法的返回类型,然后您可以将结果转换为 int:

    public class EnumList<TEnum> where TEnum:struct, IComparable
    {
        private IList<TEnum> _list;

        public EnumList()
        {
            _list = new List<TEnum>();
        }

        public void Add(TEnum val)
        {
            _list.Add(val);
        }

        public TEnum Get(TEnum val)
        {
            return _list.Single(l => l.CompareTo(val) == 0);
        }
    }

    public class MyClass
    {
        public void MyMethod()
        {
            var list = new EnumList<DayOfWeek>();
            var value = (int)list.Get(DayOfWeek.Wednesday);
        }
    }

I would change the return type of the Get method, then you can just cast the result to int:

    public class EnumList<TEnum> where TEnum:struct, IComparable
    {
        private IList<TEnum> _list;

        public EnumList()
        {
            _list = new List<TEnum>();
        }

        public void Add(TEnum val)
        {
            _list.Add(val);
        }

        public TEnum Get(TEnum val)
        {
            return _list.Single(l => l.CompareTo(val) == 0);
        }
    }

    public class MyClass
    {
        public void MyMethod()
        {
            var list = new EnumList<DayOfWeek>();
            var value = (int)list.Get(DayOfWeek.Wednesday);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文