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().In vokeMember("+", 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 1545 //What i want to do... return o1.getType().In vokeMember("+", 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().In vokeMember("+", 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.co m> 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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... 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().In vokeMember("+", 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.co m> 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().In vokeMember("+", 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.GetTypeCod e 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
========================================
SUMMARY
This PEP proposes an extension to permit objects to define their own
|
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 possibility of
overload these operators not at the global level namespace but
into different program namespaces.
For instances:
#include<iostream>
using namespace std;
|
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 a bit of a problem in seeing what needs to happen
here. I am just not sure how I do this. Will the overloading function
recognize a < and a usual <? Do I do an
IF (a.letters < b.letters){
return true
}
|
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 with 2's complement,
1's complement, or sign-magnitude arithmetic. But the followup
remark is sometimes also made that the choice of arithmetic isn't
completely unconstrained, since the bitwise operators seem to
presume a base-2 machine.
|
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 specific
example was to define an "outer product" operator for matrices. (There
was even a PEP, number 211, about this.)
I gave it some thought, and Googled for previous discussions about this,
and came up with this suggestion:
User-defined...
|
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 probably do is to use reflections to dynamically acess properties of the controller on the run-time. Not possible with reflections in the partial trust environment on shared host !!
So I switched to methods of System.ComponentModel and use right...
|
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 that I interface with Reflections. I need to run the following command (BTSAggregate1:BTSPort-xx) and then have an input box popup to ask for the value of the "xx" in the command and then execute the command.
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |