13

我在这里想要实现的是盒装原始类型的直接值比较。

((object)12).Equals((object)12); // Type match will result in a value comparison,
((object)12).Equals((object)12d); // but a type mismatch will not. (false)
object.Equals((object)12,(object)12d); // Same here. (false)

我明白“为什么”。我只是没有看到“如何”。

这些类型在运行时之前是未知的,它们可以是来自数据源的任何原始类型。这包括字符串、日期时间、布尔值等。我已经走了一条丑陋的路线,编写了一个可以解决两种类型的扩展方法,然后在进行“==”比较之前进行强制转换:(为了完整起见,我包括了每个原始类型,加上我感兴趣的那些)

public static bool ValueEquals(this object thisObj, object compare)
    {
        if (thisObj is int)
        {
            int obj = (int)thisObj;
            if (compare is int)
                return (obj == (int)compare);
            if (compare is uint)
                return (obj == (uint)compare);
            if (compare is decimal)
                return (obj == (decimal)compare);
            if (compare is float)
                return (obj == (float)compare);
            <... and so on for each primitive type ...>
        }
        if (thisObj is uint)
        {
            uint obj = (uint)thisObj;
            if (compare is int)
                return (obj == (int)compare);
            if (compare is uint)
                return (obj == (uint)compare);
            <... Again for each primitive type ...>
        }
        if (thisObj is decimal)
        {
            decimal obj = (decimal)thisObj;
            if (compare is int)
                return (obj == (int)compare);
            <... Etc, etc ...>

结果方法结果是 300 多行,这很好(但很可怕),但现在我需要做的不仅仅是“==”。我需要 >、<、<=、>=、!=。

反射中有什么可以用于盒装值类型比较的吗?

有什么吗?

4

2 回答 2

8

看起来你假设来自 arg1 的类型是你想要转换的类型,所以我会使用这样的泛型。只要 arg2 是 IConvertible(int、double、所有数字、字符串等都是 IConvertible),这将起作用:

public static bool ValueEquality<T1, T2>(T1 val1, T2 val2) 
    where T1 : IConvertible 
    where T2 : IConvertible
{
    // convert val2 to type of val1.
    T1 boxed2 = (T1) Convert.ChangeType(val2, typeof (T1));

    // compare now that same type.
    return val1.Equals(boxed2);
}

** 更新 ** 使这两种类型都成为通用 args,都可以推断并在 arg2 上增加更多的编译时安全性,以确保它在编译时是 IConvertible。

鉴于此通用函数,以下所有内容现在都返回 true(不需要指定类型参数,因为从第一个参数推断:

        Console.WriteLine(ValueEquality(1, "1"));
        Console.WriteLine(ValueEquality(2, 2.0));
        Console.WriteLine(ValueEquality(3, 3L));

更新

根据您的评论,如果您拥有的只是对象,那么这就是一个重载。两者可以共存,它会根据参数调用更合适的一个:

    public static bool ValueEquality(object val1, object val2)
    {
        if (!(val1 is IConvertible)) throw new ArgumentException("val1 must be IConvertible type");
        if (!(val2 is IConvertible)) throw new ArgumentException("val2 must be IConvertible type");

        // convert val2 to type of val1.
        var converted2 = Convert.ChangeType(val2, val1.GetType());

        // compare now that same type.
        return val1.Equals(converted2);
    }

这将适用于对象:

        object obj1 = 1;
        object obj2 = 1.0;

        Console.WriteLine(ValueEquality(obj1, obj2));

正如我所说,这两者可以作为重载共存,所以如果你直接比较兼容的 IConvertible 类型,它将使用泛型,如果你只是将装箱类型作为对象,它将使用对象重载。

于 2011-07-12T18:17:30.157 回答
3

研究使用 IComparable 而不是手动 if's - http://msdn.microsoft.com/en-us/library/system.icomparable.compareto.aspx

如果您将来需要类似的东西,请首先考虑切换一个操作数的类型,并为每种类型实现“操作处理程序”类,并使用方法来处理类似IntOpHandler.PerformOp(int left, object right).

您通常还可以通过首先将多种类型合并到一起来减少需要处理的类型数量(即字节、短、ushort、int、uint、long - 先转换为长,然后对长执行操作)。

于 2011-07-12T18:18:04.993 回答