Executing operators through reflections

Y

Yahoo

I have an object being passed into a method that I know implements the +
operator. How can I execute this method if I dont know its type?

What I want to do is...

public object Add(object o1, object o2)
{
//what needs to happen
//return o1 + o2;

//What i want to do...
return o1.getType().InvokeMember("+", new object(){o1, o2});
}



o1 and o2 would be both either a double, int, string, long, or my own
classes that implement the + operator.
 
M

Mattias Sjögren

//What i want to do...
return o1.getType().InvokeMember("+", new object(){o1, o2});
}

Custom + operators are implemented as methods called op_Addition.

o1 and o2 would be both either a double, int, string, long, or my own
classes that implement the + operator.

You'd have to special case the primitive types.


Mattias
 
J

Jon Skeet [C# MVP]

Yahoo said:
I have an object being passed into a method that I know implements the +
operator. How can I execute this method if I dont know its type?

What I want to do is...

public object Add(object o1, object o2)
{
//what needs to happen
//return o1 + o2;

//What i want to do...
return o1.getType().InvokeMember("+", new object(){o1, o2});
}

o1 and o2 would be both either a double, int, string, long, or my own
classes that implement the + operator.

String itself doesn't implement the "+" operator - it's done by the
compiler. Double/int/long etc have MSIL instructions specifically to
add them, so again the work is done by the compiler. For your own
classes, I believe you need to find the op_Add method (or possibly
op_add - look at such a class with reflector).
 
Y

Yahoo

I dont want to have a list of if statements to properly handle this. Is
there another way to solve this kind of problem?

Jon Skeet said:
String itself doesn't implement the "+" operator - it's done by the
compiler. Double/int/long etc have MSIL instructions specifically to
add them, so again the work is done by the compiler. For your own
classes, I believe you need to find the op_Add method (or possibly
op_add - look at such a class with reflector).
--
Jon Skeet - <[email protected]>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


//What i want to do...
return o1.getType().InvokeMember("+", new object(){o1, o2});
}
 
J

Jon Skeet [C# MVP]

Yahoo said:
I dont want to have a list of if statements to properly handle this. Is
there another way to solve this kind of problem?

Not that I know of, no.

You only need to have it in a single place though. Isolated ugliness is
still ugly, but it's a lot better than ugliness you need to distribute
through your code :)

(You could use Type.GetTypeCode and a switch statement to make it
slightly nicer, but it's still effectively special-casing each type.)

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top