gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:16:28 24 4
gpt4 key购买 nike

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

根据 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com