简体   繁体   中英

C# Invoke operators in generics on dotnet 2.0

I'm trying to implement these on 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.
I want to implement it with framework dotnet 2.0 .

Is there away to implement it?

Reflection API exists since .NET 1.0; 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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