473,396 Members | 2,002 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,396 software developers and data experts.

Generic Comparer Object

Hi All,

I have created my own generic comparer object which implements IComparer and
uses reflection. I have tested it and it seem to work fine but I am worried
about the way I did it. My goal was to not have to make a seperate Comparer
class for every field of my object that I wanted to sort like the ASP.net
TimeTracker starter kit did. My Code is below. Basically to use it all I
have to do is to Create the GenericComparer object, set the CompareType and
CompareField properties, and pass it to the base.sort function of my custom
collection object. here is an example:

GenericComparer gcobj = new GenericComparer();
gcobj.CompareType = typeof(MyCustomObject);
gcobj.CompareField("PropertyName");
base.sort(gcobj);

I have tested speed on 1000 plus items in my collection
and it seem to be just as fast as making a separate Comparer Classes for
each field. I realize that the Framework will call my Compare method for
each object in the collection so should I be worried about using reflection
code in the method? Is calling reflection code processor intensive?
Please let me know if you have any opinions on the way I implemented this,
I want to learn the best way to do things.

Thanks

--
Frank Wisniewski MCSE 4.0, MCP+I, A+
f p w 2 3 @ h o t m a i l . c o m

public class GenericComparer : IComparer
{
private Type _CompareType;
private string _CompareField;

public GenericComparer()
{}

public Type CompareType
{
get
{
return _CompareType;
}
set
{
_CompareType = value;
}
}

public string CompareField
{
get
{
return _CompareField;
}
set
{
_CompareField = value;
}
}

public int Compare(object x, object y)
{
int ret;
object[] tempParams = new object[1];
tempParams[0] = y.GetType().GetProperty(_CompareField).GetValue(y,
null);
object tempProp = x.GetType().GetProperty(_CompareField).GetValue(x,
null);
ret = (int) tempProp.GetType().InvokeMember("CompareTo",
BindingFlags.InvokeMethod, null, tempProp, tempParams);
return ret;
}
}

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #1
0 1794

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

Similar topics

0
by: Frank Wisniewski | last post by:
Hi All, I have created my own generic comparer object which implements IComparer and uses reflection. I have tested it and it seem to work fine but I am worried about the way I did it. My goal...
12
by: Elhanan | last post by:
hi.. i wanted to build a Dictionary Classs that will load my own class called letter, i understood that i implement the IEquatable interface's equles method that then the dictionary would use...
4
by: Adam Clauss | last post by:
I ran into a problem a while back when attempting to convert existing .NET 1.1 based code to .NET 2.0 using Generic collections rather than Hashtable, ArrayList, etc. I ran into an issue because...
3
by: kim.nolsoee | last post by:
Hi I want to use the Dictionary Classs that will load my own class called KeyClass used as TKey. Here is the code: public class Dictionary { public static void Main()
7
by: Abhishek | last post by:
Hi I am using a generic list in my program to have a list of objects stored. I want to compare all the objects in this list with object of another class and find all the objects which meet the...
9
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have a generic list that is populated with a structure. I would like to be able to sort on values in the structure. I have public struct mergoutstruct { public Int32 projectnumber; public...
2
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have a generic list populated by a structure and sorts the contents of the list based on the day and project # that is part of the structure. I would like to modify it to also sort on the...
4
by: colin | last post by:
Hi, Im trying to implement a generic list class for a number of 3d object types, that form a 3d solid model, take the easiest one Point for example, it contains a Vector3 and a list of other...
10
by: Tony | last post by:
Hello! I'm reading in a book and this can't be correct it says. "Objects passed to Comparer.Compare() are checked to see if they support IComparable. If they do, then that implementation is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...

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.