473,485 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Executing operators through reflections

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.


Nov 17 '05 #1
4 1540
//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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #2
Yahoo <jo***************@yahoo.com> wrote:
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
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 [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Yahoo <jo***************@yahoo.com> wrote:
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).

--
Jon Skeet - <sk***@pobox.com>
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});
} 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


Nov 17 '05 #4
Yahoo wrote:
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

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2481
by: greg | last post by:
Discussion is invited on the following proto-PEP. ------------------------------------------------------------- PEP ??? - Overloadable Boolean Operators...
4
2020
by: GianGuz | last post by:
Global new and delete operators can be overloaded to suite particulars needs. Typically they are overloaded to insert useful debugging/trace informations. What I would to discuss here concerns the...
20
1786
by: KL | last post by:
I am working on a school assignment, so please don't tell me the solution. I just want some direction. I am supposed to overload the >, <, ==, !=, >=, and <= operators using bool. I am having...
2
3445
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
49
14854
by: raju | last post by:
hi can we compare two integers without using relational operators (== != < <= > >=) thanks rajesh s
0
1704
by: Syanide | last post by:
here's a bit info for you fellas: eg.. you have Square classes u wanna add public static int operator+ (Square s1, Square s2) { // your code here } comparision operators..
17
2418
by: Steve R. Hastings | last post by:
I have been studying Python recently, and I read a comment on one web page that said something like "the people using Python for heavy math really wish they could define their own operators". The...
0
1259
by: arturbl | last post by:
I just finished my sort of MVC/P framework where I do url rewriting. During the url rewriting I access properties of the controller/presenter based on the url. First thing that most of us would...
0
1634
by: Robertson1995 | last post by:
I have been using Reflections for a while and I am familiar with macros, but am new to VBA. I need a VBA code that I think is fairly simple, but being new I need help. I have a piece of equipment...
0
7090
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6960
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7116
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6825
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4857
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3058
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3063
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
595
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
247
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.