compare two object

  • Thread starter Thread starter Locia
  • Start date Start date
L

Locia

How can I understand if two object obj1 and obj2 have same type?

Can I do: if (obj1.GetType()==obj2.GetType()) return true;?

Moto u1;
Moto u2;

I call Compare(u1,u2,">")

class Moto have override operator >.

How can I use reflection in Compare fuction to call operator > of Moto
class to compare u1 and u2?


bool Compare(Object obj1,Object obj2,String Operator)
{

.....?
}
 
Locia said:
How can I understand if two object obj1 and obj2 have same type?

Can I do: if (obj1.GetType()==obj2.GetType()) return true;?
Absolutely.

Moto u1;
Moto u2;

I call Compare(u1,u2,">")

class Moto have override operator >.

How can I use reflection in Compare fuction to call operator > of Moto
class to compare u1 and u2?

MethodInfo method = typeof(Moto).GetMethod("op_GreaterThan",
BindingFlags.Public |
BindingFlags.Static);

That seems to work, although I don't know to what extent it's
guaranteed...
 
I can't use "typeof(Moto)" in Compare function because u1 and u2 can be
two instance of another class.
 
I can't use "typeof(Moto)" in Compare function because u1 and u2 can be
two instance of another class.
Another way?
 
I can't use "typeof(Moto)" in Compare function because u1 and u2 can be
instance of other class.
u1 and u2 are generic object.
Another way?
 
Locia said:
I can't use "typeof(Moto)" in Compare function because u1 and u2 can be
instance of other class.
u1 and u2 are generic object.
Another way?

Call GetType() on u1 then.
 

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

Back
Top