473,563 Members | 2,696 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inspect type for operators and use them?

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 variables that
need to be compared, but they are boxed as type object. The only way I
have found that I can currently compare them is to use a case statement
to determine their type, cast them to the type, and then compare them.
This would be find if I only had to deal with the stock value types, but
there are custom types in the application, and that would mean writing
the necessary checks for each custom type.

Is it possible to do it all automagically?

Thanks,
Steve
Nov 16 '05 #1
2 1148
I would take a different approach to this problem.

What you're really after, I submit, is a test to see if the items in
your list implement the IComparable interface, either as you put them
in the list, or when you're trying to compare them. (The former would
be prefereable.)

Once you know that a value or class implements IComparable, you can
call the CompareTo() method to compare them.

True, this doesn't call the operator overload methods, but then those
methods should be impelemented in terms of CompareTo(), anyway. Any
type that has comparison operators but doesn't implement IComparable
deserves a second look.

For example, I threw together this quick little application:

static void Main(string[] args)
{
int i = 5;
int j = 6;
ArrayList list = new ArrayList();
list.Add(i);
list.Add(j);
Console.WriteLi ne("{0} compared to {1} = {2}", list[0], list[1],
((IComparable)l ist[0]).CompareTo(lis t[1]));
}

As you can see, even boxed types compare just fine.

Now, if you need to test whether an object implements IComparable, you
can use Reflection:

Type inter = list[0].GetType().GetI nterface("IComp arable");
if (inter == null)
{
Console.WriteLi ne("Does not implement IComparable");
}
else
{
Console.WriteLi ne("Implements IComparable");
}
No switch statement needed!

Just for fun, I also created my own little struct (a value type) that
implements IComparable, and put it in an ArrayList (and thus boxed it).
Sure enough, I could compare the ArrayList elements like before and it
all worked.

Nov 16 '05 #2
> Now, if you need to test whether an object implements IComparable, you
can use Reflection:

Type inter = list[0].GetType().GetI nterface("IComp arable");


Or just:
IComparable comp = list[0] as Comparable;
if (comp != null)
{
// supports IComparable
}

Also, to compare two objects using the default operators, use
Comparer.Defaul t.Compare(...)
--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vk arlsen.no
PGP KeyID: 0x0270466B
Nov 16 '05 #3

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

Similar topics

11
3941
by: It's me | last post by:
I discovered the hardway what inspect.isclass() is doing. Consider this no brainer code: ### import inspect class Abc: def Hello(self): return
4
5929
by: aramsey | last post by:
I have a javascript program that works fine under Firefox and on IE when running XP, but is having a problem with IE running under Windows 2000 Pro. On my personal XP development machine I have the Microsoft Script Editor and I can set a breakpoint, step through code, inspect variables, etc... with no problem. On a machine where I am...
3
949
by: Aaron \Castironpi\ Brady | last post by:
Hi all, Found this bug. It's in 2.6, too bad. Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) on win 32 Type "help", "copyright", "credits" or "license" for more information. <type 'tuple'> Docs say:
0
2187
by: rajasankar | last post by:
Hi, I am using Jython based application and trying to use inspect.py in the python files. Here is my code import inspect,os,sys,pprint,imp def handle_stackframe_without_leak(getframe): frame = inspect.currentframe() try: function = inspect.getframeinfo(getframe)
0
7583
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...
0
8106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7638
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...
0
7948
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 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...
0
6250
agi2029
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...
0
3642
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...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
923
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.