5

在下面的代码中,Resharper 给了我一个警告Cannot cast expression of type 'Color' to type 'UIntPtr':(实际上,Resharper 认为这是一个实际错误。)

但是,没有编译器警告,它工作正常。

对我来说,这看起来像是一个 Resharper 错误。是吗?还是编译器不担心它有什么不好的地方?(我正在使用 Resharper 7.1.1)

using System;

namespace Demo
{
    internal class Program
    {
        public enum Color { Red, Green, Blue }

        private static void Main(string[] args)
        {
            UIntPtr test = (UIntPtr) Color.Red; // Resharper warning, no compile warning.
        }
    }
}

我可以通过先将值转换为 int 来消除警告,所以我有一个解决方法:

UIntPtr test = (UIntPtr)(int) Color.Red;
4

1 回答 1

3

对我来说,这看起来像是一个 Resharper 错误。是吗?

是的

RSRP-78748 False '转换不存在' (UIntPtr)

using System;

class A
{
    static void Main()
    {
        E? x = 0;
        UIntPtr z = (UIntPtr)x;
    }
}
enum E { }

这是一个已知的规格偏差。

截至 2013 年 3 月 5 日未修复。

于 2013-03-05T14:22:57.273 回答