Click here to Skip to main content
15,895,746 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: I hate it when I am too clever for my own good... Pin
OriginalGriff10-Feb-14 0:59
mveOriginalGriff10-Feb-14 0:59 
GeneralRe: I hate it when I am too clever for my own good... Pin
Gary Wheeler10-Feb-14 1:15
Gary Wheeler10-Feb-14 1:15 
GeneralRe: I hate it when I am too clever for my own good... Pin
OriginalGriff10-Feb-14 1:23
mveOriginalGriff10-Feb-14 1:23 
GeneralRe: I hate it when I am too clever for my own good... Pin
Gary Wheeler10-Feb-14 1:34
Gary Wheeler10-Feb-14 1:34 
GeneralRe: I hate it when I am too clever for my own good... Pin
Simon O'Riordan from UK10-Feb-14 5:09
Simon O'Riordan from UK10-Feb-14 5:09 
GeneralRe: I hate it when I am too clever for my own good... Pin
OriginalGriff10-Feb-14 5:32
mveOriginalGriff10-Feb-14 5:32 
GeneralRe: I hate it when I am too clever for my own good... Pin
James Curran10-Feb-14 1:40
James Curran10-Feb-14 1:40 
GeneralRe: I hate it when I am too clever for my own good... Pin
Richard Deeming10-Feb-14 3:48
mveRichard Deeming10-Feb-14 3:48 
You can use the System.Linq.Expressions namespace to create generic operators quite easily. For example, see Marc Gravell's Generic Operators[^] from the MiscUtil library[^].

C#
public static class GenericOperator<T>
{
    private static Func<T, T, TResult> CreateCore<TResult>(Func<Expression, Expression, BinaryExpression> body)
    {
        try
        {
            Type typeT = typeof(T);
            var left = Expression.Parameter(typeT, "left");
            var right = Expression.Parameter(typeT, "right");
 
            if (typeT.IsEnum)
            {
                Type enumType = Enum.GetUnderlyingType(typeT);
                var x = Expression.Convert(left, enumType);
                var y = Expression.Convert(right, enumType);
 
                Expression op = body(x, y);
                if (op.Type == enumType) op = Expression.Convert(op, typeT);
 
                return Expression.Lambda<Func<T, T, TResult>>(op, left, right).Compile();
            }
 
            return Expression.Lambda<Func<T, T, TResult>>(body(left, right), left, right).Compile();
        }
        catch (InvalidOperationException ex)
        {
            string message = ex.Message;
            return delegate { throw new InvalidOperationException(message); };
        }
        catch (ArgumentException ex)
        {
            string message = ex.Message;
            return delegate { throw new InvalidOperationException(message); };
        }
    }
 
    private static Lazy<Func<T, T, TResult>> Create<TResult>(Func<Expression, Expression, BinaryExpression> body)
    {
        return new Lazy<Func<T, T, TResult>>(() => CreateCore<TResult>(body), true);
    }
 
    private static readonly Lazy<Func<T, T, T>> _add = Create<T>(Expression.Add);
 
    public static Func<T, T, T> Add
    {
        get { return _add.Value; }
    }
 
    ...
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: I hate it when I am too clever for my own good... Pin
Simon O'Riordan from UK10-Feb-14 5:07
Simon O'Riordan from UK10-Feb-14 5:07 
GeneralRe: I hate it when I am too clever for my own good... Pin
OriginalGriff10-Feb-14 5:30
mveOriginalGriff10-Feb-14 5:30 
GeneralRe: I hate it when I am too clever for my own good... Pin
RafagaX10-Feb-14 9:40
professionalRafagaX10-Feb-14 9:40 
GeneralRe: I hate it when I am too clever for my own good... Pin
Moreno Airoldi12-Feb-14 5:50
Moreno Airoldi12-Feb-14 5:50 
GeneralRick York share your dissatisfaction Pin
Sanmayce9-Feb-14 4:45
Sanmayce9-Feb-14 4:45 
GeneralRe: Rick York share your dissatisfaction Pin
OriginalGriff9-Feb-14 5:02
mveOriginalGriff9-Feb-14 5:02 
GeneralRe: Rick York share your dissatisfaction Pin
Sanmayce9-Feb-14 5:18
Sanmayce9-Feb-14 5:18 
GeneralRe: Rick York share your dissatisfaction Pin
R. Giskard Reventlov9-Feb-14 5:47
R. Giskard Reventlov9-Feb-14 5:47 
GeneralRe: Rick York share your dissatisfaction Pin
Sanmayce9-Feb-14 6:02
Sanmayce9-Feb-14 6:02 
GeneralRe: Rick York share your dissatisfaction Pin
R. Giskard Reventlov9-Feb-14 6:10
R. Giskard Reventlov9-Feb-14 6:10 
GeneralRe: Rick York share your dissatisfaction Pin
Sanmayce9-Feb-14 6:42
Sanmayce9-Feb-14 6:42 
GeneralRe: Rick York share your dissatisfaction Pin
R. Giskard Reventlov9-Feb-14 11:36
R. Giskard Reventlov9-Feb-14 11:36 
GeneralRe: Rick York share your dissatisfaction Pin
Richard MacCutchan9-Feb-14 6:16
mveRichard MacCutchan9-Feb-14 6:16 
GeneralSnowden: Used a Can-Opener, not a Laser-Beam PinPopular
BillWoodruff8-Feb-14 22:16
professionalBillWoodruff8-Feb-14 22:16 
GeneralRe: Snowden: Used a Can-Opener, not a Laser-Beam Pin
Mike Hankey8-Feb-14 22:53
mveMike Hankey8-Feb-14 22:53 
GeneralRe: Snowden: Used a Can-Opener, not a Laser-Beam Pin
JimmyRopes9-Feb-14 7:05
professionalJimmyRopes9-Feb-14 7:05 
GeneralOT: AVR Programmer Pin
Richard Andrew x649-Feb-14 15:47
professionalRichard Andrew x649-Feb-14 15:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.