473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to use operators on object acquired by reflection ?

I have two object that has been acquired using reflection as follow:

string str = "123";
object param1 = dataRow["myRow"];
System.Type paramType = param1.GetType();
System.Reflection.MethodInfo Parse = paramType.GetMethod("Parse", new Type[]
{typeof(String)});
object param2 = Parse.Invoke(param1, new object[] { str });

Now I have two objects: param1 and param2.
Both params are of the same type.

Now I wish to compare this two using the >, <, <= etc.
But I manage to use only the == and != operators on them.

Can anybody How can I use >, <, <= , >= on the param1 and param2 above ?
-----------
Thanks
Sharon
Jun 8 '06 #1
6 1438
"Sharon" <Sh*****@newsgroups.nospam> a écrit dans le message de news:
81**********************************@microsoft.com...

| Now I have two objects: param1 and param2.
| Both params are of the same type.
|
| Now I wish to compare this two using the >, <, <= etc.
| But I manage to use only the == and != operators on them.
|
| Can anybody How can I use >, <, <= , >= on the param1 and param2 above ?

I would thnk you can only do this by casting them to the appropriate type
that supports those operators.

Operators are sort of static methods that operate only on the type in which
they are declared.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jun 8 '06 #2
"Sharon" <Sh*****@newsgroups.nospam> ha scritto nel messaggio
news:810CC9A8-AA27-4633-843E-

Now I have two objects: param1 and param2.
Both params are of the same type.

Now I wish to compare this two using the >, <, <= etc.
But I manage to use only the == and != operators on them.

Can anybody How can I use >, <, <= , >= on the param1 and param2 above ?


to be compared objects have to be IComparable.

If so, just do:

param1.CompareTo(param2) > 0
param1.CompareTo(param2) < 0
param1.CompareTo(param2) != 0
etc...

Jun 8 '06 #3
Sharon,

If you know that your objects are strings or support IComparable interface
you can use the Comparer class from the System.Collections namespace.

Without having some information about the objects e.g. comon base class,
common interface or some set of expected types it might be even impossible
to implement that. Consider this not all types can be ordered, thus not all
types can be compared for < or >.

On the other hand all objects can be compared for equality whether reference
equality or internal state equality and this is the reason that the Object
type has virtual Eqauals method the can be used on any object.
--
HTH
Stoitcho Goutsev (100)

"Sharon" <Sh*****@newsgroups.nospam> wrote in message
news:81**********************************@microsof t.com...
I have two object that has been acquired using reflection as follow:

string str = "123";
object param1 = dataRow["myRow"];
System.Type paramType = param1.GetType();
System.Reflection.MethodInfo Parse = paramType.GetMethod("Parse", new
Type[]
{typeof(String)});
object param2 = Parse.Invoke(param1, new object[] { str });

Now I have two objects: param1 and param2.
Both params are of the same type.

Now I wish to compare this two using the >, <, <= etc.
But I manage to use only the == and != operators on them.

Can anybody How can I use >, <, <= , >= on the param1 and param2 above ?
-----------
Thanks
Sharon

Jun 8 '06 #4
"Fabio" <zn*******@virgilio.it> a écrit dans le message de news:
eI**************@TK2MSFTNGP03.phx.gbl...

| to be compared objects have to be IComparable.
|
| If so, just do:
|
| param1.CompareTo(param2) > 0
| param1.CompareTo(param2) < 0
| param1.CompareTo(param2) != 0
| etc...

Which means you would have to cast object instances to IComparable before
using this code. If you are not certain that the objects support
IComparable, then you should test for nil after the cast to ensure no AVs.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jun 8 '06 #5
I'm sure you are right, but I do not know what to cast it to at programming
time, I only know it at run time, and in run time I do hold the correct type
at hand, but I'm holding it using polymorphism on a parent reference type
which is object.

How can I do it in the case I posted above ?
----------
Thanks
Sharon G.
Jun 8 '06 #6
Thanks guys, both casting to IComparable and using
System.Collections.Comparer works fine.

All my parameters/object are simple/primitive type (int, long, string, float
etc.), so they all support the IComparable interface.

Thanks a lot for the quick attention.
----------
Regards
Sharon
Jun 8 '06 #7

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

Similar topics

9
by: Balaji | last post by:
Dear Jeff, Thanks for your reply. I see that the PEP and the current docs specify that __ge__ and __le__ are their own reflection. Given that PEP 207 proposes rich comparison to handle cases...
14
by: greg | last post by:
Discussion is invited on the following proto-PEP. ------------------------------------------------------------- PEP ??? - Overloadable Boolean Operators...
2
by: Steve | last post by:
Is there a way to dynamically inspect a type for the operators it implements, and then compare two objects implementing that type? Basically I'm looking at a situation where I receive two...
0
by: David Berman | last post by:
Hello, I'm receiving an error reading the machine.config file when I try to access my web application. This error came out of nowhere. After searching many sites, googelizing and searching here,...
9
by: Dennis | last post by:
When a class (myownclass) inheirits another class, how can I get an object reference to the underlyng MyBase class instance from within myownclass. The base class has a method that I want to...
28
by: dspfun | last post by:
I'm trying to get a good understanding of how unary operators work and have some questions about the following test snippets. int *p; ~!&*++p--; It doesn't compile, why? The problem seems to be...
1
by: Fawad Ahsan | last post by:
Hi i am having problem with the microsoft enterprise library. i have uploaded the site to the hosting server and it gives the following error it works well in my server. can some one help me...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
1
by: BrendanC | last post by:
I'm trying to understand reflection/introspection in Python. How can I identify the the type of attribute (e.g. instance var) in a class? The following returns all the class attributes (methods and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
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...

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.