简体   繁体   English

C# 在 dotnet 2.0 上调用泛型中的运算符

[英]C# Invoke operators in generics on dotnet 2.0

I'm trying to implement these on dotnet 2.0.我正在尝试在 dotnet 2.0 上实现这些。

static class Operator
{
    public static object Add(object left, object right)
    {
        //Do something with left + right
    }

    public static object BitwiseAnd(object left, object right)
    {
        //Do something with left | right
    }

    public static object Decrease(object value)
    {
        //Do something with value--
    }

    public static object Divide(object left, object right)
    {
        //Do something with left / right
    }

    public static object Increase(object value)
    {
        //Do something with value++
    }

    public static object Multiply(object left, object right)
    {
        //Do something with left * right
    }

    public static object Subtract(object left, object right)
    {
        //Do something with left - right
    }
}

But I'm only know it is possible with dynamic type or System.Linq.Expressions on dotnet 4.0.但我只知道在 dotnet 4.0 上使用dynamic类型System.Linq.Expressions是可能的。
I want to implement it with framework dotnet 2.0 .我想用框架dotnet 2.0实现它。

Is there away to implement it?有没有实施它?

Reflection API exists since .NET 1.0;自 .NET 1.0 起就存在反射 API it might help you.它可能会帮助你。

Reflection allows you to query for object types, list their methods and properties and invoke them.反射允许您查询对象类型,列出它们的方法和属性并调用它们。 Unfortunately your question (at the moment) doesn't indicate what precisely you intend to do with the objects, but since you are saying that you could use dynamic in newer .NET versions, reflection looks promising because it allows to achieve everything dynamics do, and more, though the resulting code gets much more verbose.不幸的是,您的问题(目前)并没有表明您打算对对象做什么,但是既然您说可以在较新的 .NET 版本中使用动态,反射看起来很有希望,因为它可以实现动态所做的一切,以及更多,尽管生成的代码变得更加冗长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM