如何对两个对象求和?

发布于 2024-12-04 16:57:08 字数 878 浏览 0 评论 0原文

我想做一个解析文本的应用程序。到目前为止,我有一个名为 Result 的类,它保存值并键入方程的每个部分。

public enum ResultType
{
    Int32,
    Double,
    Boolean,
    Color,
    DateTime,
    String,
    Undefined,
    Void
}

public class Result
{
    public object Value { get; set; }
    public ResultType Type { get; set; }
}

可能的结果可能是:

 5 : Int32
 true : Boolean 
 DADACC : Color 
 "Hello World!" : String 
 10.0 : Double 
 13/11/1986 : DateTime

现在我想求和/除/求/...两个结果,但我真的不想做所有的工作。在 C# 中,您可以将它们全部混合在一起并得到答案。

var value = "Hello" + 2.0 + 4 + DateTime.Today; (value = "Hello2413/09/2011 12:00:00 a.m.")

有没有简单的方法来处理这个问题?还是我必须自己弄清楚所有组合?我在想这样的事情:

var Operator = "+"; // or "-","*","/","^","%"
var sum = DoTheCSharpOperation(Operator, ResultA.Value, ResultB.Value)
var sumResult = new Result(sum);

I want to do an application that pareses text. So far, I have a class called Result, that holds the value and type each part of an equation.

public enum ResultType
{
    Int32,
    Double,
    Boolean,
    Color,
    DateTime,
    String,
    Undefined,
    Void
}

public class Result
{
    public object Value { get; set; }
    public ResultType Type { get; set; }
}

Possible Result's could be:

 5 : Int32
 true : Boolean 
 DADACC : Color 
 "Hello World!" : String 
 10.0 : Double 
 13/11/1986 : DateTime

Now I want to sum/divide/pow/... two Results but I really don´t want to do all the work. In C#, you can mix them all together and get an answer.

var value = "Hello" + 2.0 + 4 + DateTime.Today; (value = "Hello2413/09/2011 12:00:00 a.m.")

Is there an easy way to handle this? Or do I have to figure out all combos by myself? I´m thinking about something like:

var Operator = "+"; // or "-","*","/","^","%"
var sum = DoTheCSharpOperation(Operator, ResultA.Value, ResultB.Value)
var sumResult = new Result(sum);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场信仰旅途 2024-12-11 16:57:08

在我看来,这听起来像是“dynamic”关键字的完美应用:

using System;
using System.Diagnostics;

namespace ConsoleApplication33 {
  public static class Program {
    private static void Main() {
      var result1=DoTheCSharpOperation(Operator.Plus, 1.2, 2.4);
      var result2=DoTheCSharpOperation(Operator.Plus, "Hello", 2.4);
      var result3=DoTheCSharpOperation(Operator.Minus, 5, 2); 

      Debug.WriteLine(result1); //a double with value 3.6
      Debug.WriteLine(result2); //a string with value "Hello2.4"
      Debug.WriteLine(result3); //an int with value 3
    }

    public enum Operator {
      Plus,
      Minus
    }

    public static object DoTheCSharpOperation(Operator op, dynamic a, dynamic b) {
      switch(op) {
        case Operator.Plus:
          return a+b;
        case Operator.Minus:
          return a-b;
        default:
          throw new Exception("unknown operator "+op);
      }
    }
  }
}

This sounds to me like a perfect application for the "dynamic" keyword:

using System;
using System.Diagnostics;

namespace ConsoleApplication33 {
  public static class Program {
    private static void Main() {
      var result1=DoTheCSharpOperation(Operator.Plus, 1.2, 2.4);
      var result2=DoTheCSharpOperation(Operator.Plus, "Hello", 2.4);
      var result3=DoTheCSharpOperation(Operator.Minus, 5, 2); 

      Debug.WriteLine(result1); //a double with value 3.6
      Debug.WriteLine(result2); //a string with value "Hello2.4"
      Debug.WriteLine(result3); //an int with value 3
    }

    public enum Operator {
      Plus,
      Minus
    }

    public static object DoTheCSharpOperation(Operator op, dynamic a, dynamic b) {
      switch(op) {
        case Operator.Plus:
          return a+b;
        case Operator.Minus:
          return a-b;
        default:
          throw new Exception("unknown operator "+op);
      }
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文