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. 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.
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: greg |
last post by:
Discussion is invited on the following proto-PEP.
-------------------------------------------------------------
PEP ??? - Overloadable Boolean Operators...
|
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...
|
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...
|
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...
|
by: raju |
last post by:
hi
can we compare two integers without using relational operators (== != <
<= > >=)
thanks
rajesh s
| |
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..
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |