How to use operators on object acquired by reflection ?

G

Guest

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 ?
 
J

Joanna Carter [TeamB]

"Sharon" <[email protected]> a écrit dans le message de (e-mail address removed)...

| 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
 
F

Fabio

"Sharon" <[email protected]> ha scritto nel messaggio

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...
 
J

Joanna Carter [TeamB]

"Fabio" <[email protected]> a écrit dans le message de (e-mail address removed)...

| 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
 
G

Guest

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 ?
 
S

Stoitcho Goutsev \(100\)

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.
 
G

Guest

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.
 

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