处理不同原始类型的最有效方法

时间:2018-10-16 08:40:04

标签: c# types type-conversion comparison

我正在制作一款游戏,其中包括允许玩家选择不同的选项和操作,然后执行它们以得出真假(然后根据结果执行其他操作)。

(作为程序员)我最终得到的是两个对象和一个以某种方式比较它们的请求。

public object arg0;
public CompareType compareType;
public object arg1;

args可以是intfloatstringbool。假设CompareType是诸如GREATER_THANEQUALSLESS_OR_EQUAL_THAN等的选项。默认情况下,不可比类型将转换为string然后进行比较(并且可能假)。

我无法提出解决这些问题的正确方法,以便在比较结束时获得truefalse,而不必写出每种可能性。

除了强制转换和显式转换以外,是否还有一种更通用的将类型转换为“最相似的类型”的方法?

之后,这只是基于CompareType的切换。

switch(compareType)
{ 
    case CompareTypes.EQUALS:
        return compare0 == compare1;

    case CompareTypes.GREATER_THAN: 
        return compare0 > compare1;

    case CompareTypes.LESS_OR_EQUAL_THAN: 
        return compare0 <= compare1;

    // etc.

    default:
        return false;
}

编辑:

感谢大家的回答和评论。

对于任何想知道的人。这是我想出的测试程序,基于最佳答案的99%(必须对其进行一些更改才能在在线编译器中工作):

using System;

public class Program
{
    public static void Main()
    {
        WriteResult("1", CompareType.Equals, 1);
        WriteResult(5, CompareType.Equals, 1);
        WriteResult("2", CompareType.Equals, 1);
        WriteResult("2", CompareType.GreaterThan, 1);
        WriteResult("2", CompareType.LessThan, 1);
        WriteResult(2, CompareType.GreaterThan, "1");
        WriteResult(2, CompareType.LessThan, "1");
        WriteResult("1", CompareType.Equals, "2");
        WriteResult(1, CompareType.Equals, 1.0f);
        WriteResult(1, CompareType.LessThan, 2.0f);
    }

    public static void WriteResult(IComparable left, CompareType compareType, IComparable right)
    {
        Comparison comparison = new Comparison(left, compareType, right);

        Console.WriteLine(left.GetType() + " " + left.ToString() + " - " + 
                          compareType.ToString() + " - " + 
                          right.GetType() + " " + right.ToString() + " = " + 
                          comparison.Excecute().ToString());
    }

    public class Comparison
    {
        public IComparable Left { get; set; }
        public CompareType CompareType { get; set; }
        public IComparable Right { get; set; }

        public Comparison(IComparable left, CompareType compareType, IComparable right) 
        {
            Left = left;
            CompareType = compareType;
            Right = right;
        }

        public bool Excecute()
        {
            switch (CompareType)
            {
                case CompareType.Equals:
                    return IsTypeMatch(Left, Right) ? Left.CompareTo(Right) == 0 : Left.ToString().Equals(Right.ToString());
                case CompareType.NotEquals:
                    return IsTypeMatch(Left, Right) ? Left.CompareTo(Right) != 0 : !Left.ToString().Equals(Right.ToString());
                case CompareType.GreaterThan:
                    return IsTypeMatch(Left, Right) ? Left.CompareTo(Right) != 0 : Left.ToString().CompareTo(Right.ToString()) > 0;
                case CompareType.LessThan:
                    return IsTypeMatch(Left, Right) ? Left.CompareTo(Right) != 0 : Left.ToString().CompareTo(Right.ToString()) < 0;
                default:
                    throw new NotImplementedException();
            }
        }

        //private bool IsTypeMatch => Left.GetType().Equals(Right.GetType());

        private bool IsTypeMatch(IComparable Left, IComparable Right) 
        {
            return Left.GetType().Equals(Right.GetType());
        }
    }
}

public enum CompareType
{
    Equals = 1,
    NotEquals = 2,
    GreaterThan = 3,
    LessThan = 4,
}

结果:

System.String 1 - Equals      - System.Int32  1 = True
System.Int32  5 - Equals      - System.Int32  1 = False
System.String 2 - Equals      - System.Int32  1 = False
System.String 2 - GreaterThan - System.Int32  1 = True
System.String 2 - LessThan    - System.Int32  1 = False
System.Int32  2 - GreaterThan - System.String 1 = True
System.Int32  2 - LessThan    - System.String 1 = False
System.String 1 - Equals      - System.String 2 = False
System.Int32  1 - Equals      - System.Single 1 = True
System.Int32  1 - LessThan    - System.Single 2 = True

老实说,我不确定string "2"大于string "1",但是我可能不知道如何比较字符串。转到MSDN ...

3 个答案:

答案 0 :(得分:1)

您可以使用IComparable界面作为参数。

示例:

public class Comparison
{
    public IComparable Left { get; }
    public CompareType CompareType { get; }
    public IComparable Right { get; }

    public Comparison(IComparable left, CompareType compareType, IComparable right) 
    {
        Left = left;
        CompareType = compareType;
        Right = right;
    }

    public bool Excecute()
    {
        switch (CompareType)
        {
            case CompareType.Equals:
                return IsTypeMatch ? Left.CompareTo(Right) == 0 : Left.ToString().Equals(Right.ToString());
            case CompareType.NotEquals:
                return IsTypeMatch ? Left.CompareTo(Right) != 0 : !Left.ToString().Equals(Right.ToString());
            case CompareType.GreaterThan:
                return IsTypeMatch ? Left.CompareTo(Right) != 0 : Left.ToString().CompareTo(Right.ToString()) > 0;
            case CompareType.LessThan:
                return IsTypeMatch ? Left.CompareTo(Right) != 0 : Left.ToString().CompareTo(Right.ToString()) < 0;
            default:
                throw new NotImplementedException();
        }
    }

    private bool IsTypeMatch => Left.GetType().Equals(Right.GetType());
}

public enum CompareType
{
    Equals = 1,
    NotEquals = 2,
    GreaterThan = 3,
    LessThan = 4,
}

这可以称为:

new Comparison("1", CompareType.Equals, 1).Excecute(); // returns true

new Comparison(2, CompareType.GreaterThan, 1).Excecute(); // returns true

等...

答案 1 :(得分:0)

如果仅考虑原始数据类型(由于字符串不是原始数据,则不包括字符串),则始终可以将compare0和compare1强制转换为double,然后进行比较。

类似这样的东西:

switch(compareType)
{ 
case CompareTypes.EQUALS:
    return (double)compare0 == (double)compare1;

case CompareTypes.GREATER_THAN: 
    return (double)compare0 > (double)compare1;

case CompareTypes.LESS_OR_EQUAL_THAN: 
    return (double)compare0 <= (double)compare1;
// etc.
default:
    return false;
}

但是,如果您需要包含字符串,那么我相信您应该单独处理它们。无论如何,您只能对字符串执行==或!=。

答案 2 :(得分:-1)