C# - is 运算符 - 检查所有可用转换的可铸性

标签 c# casting operators implicit-conversion explicit-conversion

在进一步阅读后进行编辑,将问题修改得更具体。

根据 Microsoft documentation :

An is expression evaluates to true if the provided expression is non-null, and the provided object can be cast to the provided type without causing an exception to be thrown. Otherwise, the expression evaluates to false.

这是下面的问题。

var test = (Int32)(Int16)1; // Non-null and does not cause an exception.
var test2 = (Int16)1 is Int32; // Evaluates false.

文档还指出:

Note that the is operator only considers reference conversions, boxing conversions, and unboxing conversions. Other conversions, such as user-defined conversions, are not considered.

因此,我假设它在上面不起作用,因为它是用户定义的转换。

我如何检查某些内容是否可转换为另一种类型,包括非引用/装箱/拆箱转换?

注意:我在为适用于所有类型的 CastToOrDefault 扩展编写单元测试时发现了这个问题,包括非引用类型(与 as 相比)。


基于 Mike Precup 的链接代码重构 Answer

我清理了他的答案,因为我觉得它是用旧风格写的。另外,尽管速度较慢,但​​当每个列表只是另一个列表的子集时,拥有一个包含许多重复值的表感觉有点不稳定。所以,我改为使表递归。 注意:如果值为 null,ThrowIfNull 只会抛出 ArgumentNullException。

private static readonly Dictionary<Type, IEnumerable<Type>> PrimitiveTypeTable = new Dictionary<Type, IEnumerable<Type>>
{
    { typeof(decimal), new[] { typeof(long), typeof(ulong) } },
    { typeof(double), new[] { typeof(float) } },
    { typeof(float), new[] { typeof(long), typeof(ulong) } },
    { typeof(ulong), new[] { typeof(uint) } },
    { typeof(long), new[] { typeof(int), typeof(uint) } },
    { typeof(uint), new[] { typeof(byte), typeof(ushort) } },
    { typeof(int), new[] { typeof(sbyte), typeof(short), typeof(ushort) } },
    { typeof(ushort), new[] { typeof(byte), typeof(char) } },
    { typeof(short), new[] { typeof(byte) } }
};

private static bool IsPrimitiveCastableTo(this Type fromType, Type toType)
{
    var keyTypes = new Queue<Type>(new[] { toType });
    while (keyTypes.Any())
    {
        var key = keyTypes.Dequeue();
        if (key == fromType) { return true; }
        if (PrimitiveTypeTable.ContainsKey(key)) { PrimitiveTypeTable[key].ToList().ForEach(keyTypes.Enqueue); }
    }
    return false;
}

/// <summary>
/// Determines if this type is castable to the toType.
/// This method does more than the is-operator and
/// allows for primitives and implicit/explicit conversions to be compared properly.
/// http://stackoverflow.com/a/18256885/294804
/// </summary>
/// <param name="fromType">The type to cast from.</param>
/// <param name="toType">The type to be casted to.</param>
/// <returns>True if fromType can be casted to toType. False otherwise.</returns>
/// <exception cref="ArgumentNullException">Thrown if either type is null.</exception>
public static bool IsCastableTo(this Type fromType, Type toType)
{
    // http://stackoverflow.com/a/10416231/294804
    return toType.ThrowIfNull().IsAssignableFrom(fromType.ThrowIfNull()) ||
        fromType.IsPrimitiveCastableTo(toType) ||
        fromType.GetMethods(BindingFlags.Public | BindingFlags.Static).Any(m =>
                m.ReturnType == toType && m.Name == "op_Implicit" || m.Name == "op_Explicit");
}

最佳答案

同样来自您链接的 MSDN 页面:

Note that the is operator only considers reference conversions, boxing conversions, and unboxing conversions. Other conversions, such as user-defined conversions, are not considered.

由于为 Int16Int32 类型定义的隐式转换不是引用转换、装箱转换或拆箱转换,计算结果为假。

如果您想知道为什么它不是引用转换,请注意 implicit reference conversions 页面上的以下内容:

Reference conversions, implicit or explicit, never change the referential identity of the object being converted. In other words, while a reference conversion may change the type of the reference, it never changes the type or value of the object being referred to.

由于类型正在更改,并且它不仅仅是对父类(super class)的转换,因此不被视为引用转换。

编辑:要回答您编辑的第二个问题,请查看 this page .我将复制代码以供引用:

static class TypeExtensions { 
    static Dictionary<Type, List<Type>> dict = new Dictionary<Type, List<Type>>() {
        { typeof(decimal), new List<Type> { typeof(sbyte), typeof(byte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(char) } },
        { typeof(double), new List<Type> { typeof(sbyte), typeof(byte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(char), typeof(float) } },
        { typeof(float), new List<Type> { typeof(sbyte), typeof(byte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(char) } },
        { typeof(ulong), new List<Type> { typeof(byte), typeof(ushort), typeof(uint), typeof(char) } },
        { typeof(long), new List<Type> { typeof(sbyte), typeof(byte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(char) } },
        { typeof(uint), new List<Type> { typeof(byte), typeof(ushort), typeof(char) } },
        { typeof(int), new List<Type> { typeof(sbyte), typeof(byte), typeof(short), typeof(ushort), typeof(char) } },
        { typeof(ushort), new List<Type> { typeof(byte), typeof(char) } },
        { typeof(short), new List<Type> { typeof(byte) } }
    };
    public static bool IsCastableTo(this Type from, Type to) { 
        if (to.IsAssignableFrom(from)) { 
            return true; 
        }
        if (dict.ContainsKey(to) && dict[to].Contains(from)) {
            return true;
        }
        bool castable = from.GetMethods(BindingFlags.Public | BindingFlags.Static) 
                        .Any( 
                            m => m.ReturnType == to &&  
                            m.Name == "op_Implicit" ||  
                            m.Name == "op_Explicit" 
                        ); 
        return castable; 
    } 
} 

代码使用反射来检查是否存在任何隐式或显式转换。但是,反射方法不适用于基元,因此必须手动完成检查,因此 Dictionary 可能会转换为基元。

关于C# - is 运算符 - 检查所有可用转换的可铸性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18256742/

相关文章:

c# - 了解 WCF 可靠 session 重试行为

c# - 如何在我的资源文件夹中使用 json?

c# - HttpWebRequest 未处理 "invalidcastexception"将 HttpWebResponse 转换为 System.Exception

arrays - Swift Array 如何支持有效的 Element 子类型分配?

perl - 在Perl中,美元和数字符号如何一起工作?

c# - 通过 REST API 从 TFS 获取所有成员/用户

c# - 设置一个零点,如果该值低于或高于此值,则显示为 -value 或 +value

c# - 在 C# 中使用 "Type"对象类型转换对象

c++ - 为什么 implicit == on map<<int,MyClass> 不编译?

operators - J:牛顿法的隐性副词