如何使用C#泛型把这些3种方法的人? [英] How to turn these 3 methods into one using C# generics?

查看:118
本文介绍了如何使用C#泛型把这些3种方法的人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有使用仿制药多,所以无法弄清楚是否有可能把以下三种方法为使用泛型以减少重复之一。其实我的代码,目前有六种方法,但如果你能解决这个问题的三个再剩下的应该只是反正用相同的解决办法。

I have not used generics much and so cannot figure out if it is possible to turn the following three methods into one using generics to reduce duplication. Actually my code currently has six methods but if you can solve it for the three then the rest should just work anyway with the same solution.

    private object EvaluateUInt64(UInt64 x, UInt64 y)
    {
        switch (Operation)
        {
            case BinaryOp.Add:
                return x + y;
            case BinaryOp.Subtract:
                return x - y;
            case BinaryOp.Multiply:
                return x * y;
            case BinaryOp.Divide:
                return x / y;
            case BinaryOp.Remainder:
                return x % y;
            default:
                throw new ApplicationException("error");
        }
    }

    private object EvaluateFloat(float x, float y)
    {
        switch(Operation)
        {
            case BinaryOp.Add:
                return x + y;
            case BinaryOp.Subtract:
                return x - y;
            case BinaryOp.Multiply:
                return x * y;
            case BinaryOp.Divide:
                return x / y;
            case BinaryOp.Remainder:
                return x % y;
            default:
                throw new ApplicationException("error");
        }
    }

    private object EvaluateDouble(double x, double y)
    {
        switch (Operation)
        {
            case BinaryOp.Add:
                return x + y;
            case BinaryOp.Subtract:
                return x - y;
            case BinaryOp.Multiply:
                return x * y;
            case BinaryOp.Divide:
                return x / y;
            case BinaryOp.Remainder:
                return x % y;
            default:
                throw new ApplicationException("error");
        }
    }



我建立一个简单的表达式解析器,则需要评价如加法/减法等简单的二进制运算我用上面的方法来获得实际的数学使用相关的类型进行。但一定是一个更好的答案!

I am building a simple expression parser that then needs to evaluate the simple binary operations such as addition/subtraction etc. I use the above methods to get the actual maths performed using the relevant types. But there has got to be a better answer!

推荐答案

仿制药本身不支持算术。但是,它可以使用.NET 3.5,来完成,像这样。操作符类是 MiscUtil 的一部分。这个就变成了:

Generics doesn't natively support arithmetic. However, it can be done with .NET 3.5, like so. The Operator class is part of MiscUtil. This then becomes:

   public T Evaluate<T>(T x, T y) {
      switch (Operation)
      {
        case BinaryOp.Add:
            return Operator.Add(x, y);
        case BinaryOp.Subtract:
            return Operator.Subtract(x, y);
     ... etc



既然你正在编写一个表达式解析器,它可能是一个很好的主意,直接使用表达,但欢迎您使用上面。

Since you are writing an expression parser, it might be a good idea to use Expression directly, but you're welcome to use the above.

这篇关于如何使用C#泛型把这些3种方法的人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆