Question

I want to overload operator +(f,s) for a generic class. It should return +(f,s) if f and s have plus operator and null otherwise. How can I do it?

public class param<T> : TDefault 
{
    public string param_name;
    public T cnt;
    public param(T _cnt)
    {
        cnt=_cnt;
    }
    public static param<T> operator +(param<T> f, param<T> s)
    {
        if(T ? has_operator("+"))
            return param<T>(((f.cnt as ?)+(s.cnt as ?)) as T);
        return null;
    }
}

For checking operator existence I tried

public static bool has_method(this object target,string method_name)
{
    return target.GetType().GetMethod(method_name)!=null;
}

but

int x;
print (x.has_method("+="));

prints 'false'

Was it helpful?

Solution

Try this:

public class Param<T>
{
    private static readonly Func<T, T, T> addMethod;
    static Param()
    {
        try
        {
            ParameterExpression left = Expression.Parameter(typeof (T), "left");
            ParameterExpression right = Expression.Parameter(typeof (T), "right");
            addMethod = Expression.Lambda<Func<T, T, T>>(Expression.Add(left, right), left, right).Compile();
        }
        catch (InvalidOperationException)
        {
            //Eat the exception, no + operator defined :(
        }
    }

    public string param_name;
    public T cnt;
    public Param(T _cnt)
    {
        cnt = _cnt;
    }

    public static Param<T> operator +(Param<T> leftOperand, Param<T> rightOperand)
    {
        if (addMethod != null)
        {
            return new Param<T>(addMethod(leftOperand.cnt, rightOperand.cnt));
        }
        return null;
    }
}

private static void Main(string[] args)
{
    var pi = new Param<int>(5);
    var pi2 = new Param<int>(6);
    var pi3 = pi + pi2;

    var pi4 = new Param<ooject>(5);//Wont work
    var pi5 = new Param<object>(6);
    var pi6 = pi4 + pi5;
}

Credit goes to JonSkeet and Marc

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top