Hi,
I found this code somewhere on the net and need to make some alterations.
public class GenericSorter : IComparer
{
String sortProperty;
bool sortOrder;
public GenericSorter(S tring sortBy, bool asc)
{
sortProperty = sortBy;
sortOrder = asc;
}
public int Compare(object x, object y)
{
IComparable ic1 =
(IComparable)x. GetType().GetPr operty(sortProp erty).GetValue( x,null);
IComparable ic2 =
(IComparable)y. GetType().GetPr operty(sortProp erty).GetValue( y,null);
if(sortOrder)
return ic1.CompareTo(i c2);
else
return ic2.CompareTo(i c1);
}
}
It works fine if the property to sort on is in the current object, for
example ID. However, if I pass sortProperty string such as
"thisDepartment .ID" (in order to sort by the department that the object
belings to).
Is there a way to use GetProperty to evaulate and navigate to the property
in question and then get its value?
So far I have come up with the follwing solutions.
1.) Call GetProperties() and find the first property, search for another '.'
if there isnt one then get the value of the string after the '.'. If there
is another '.' get the propoerties for that property, and so on with
recursion. Bit of a killer in terms of performance, but how else can you
provide the ability to sort on any property in an object model?
2.) Scrap the generic sorter and implement a sort for each property I want
to sort on that does the navigation. requires lots of code.
Any ideas would be cool.
TIA
MattC 3 5153
Hi,
You have to recurse, find a similar code below with that taken into account
The first if check for a "." in the property used for comparision, if its
found then it meants it's part of a complex member and it simply call it
recursely
int Compare( object x, object y, string comparer)
{
if ( comparer.IndexO f( ".") != -1 )
{
//split the string
string[] parts = comparer.Split( new char[]{ '.'} );
return Compare( x.GetType().Get Property( parts[0]).GetValue(x, null) ,
y.GetType().Get Property( parts[0]).GetValue(y, null) , parts[1]
);
}
else
{
IComparable icx, icy;
icx =
(IComparable)x. GetType().GetPr operty( comparer).GetVa lue(x, null);
icy =
(IComparable)y. GetType().GetPr operty( comparer).GetVa lue(y, null);
if ( x.GetType().Get Property(compar er).PropertyTyp e ==
typeof(System.S tring) )
{
icx = (IComparable) icx.ToString(). ToUpper();
icy = (IComparable) icy.ToString(). ToUpper();
}
if(this.sortDir ection == SortDirection.D escending)
return icy.CompareTo(i cx);
else
return icx.CompareTo(i cy);
}
}
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"MattC" <m@m.com> wrote in message
news:OP******** ******@TK2MSFTN GP12.phx.gbl... Hi,
I found this code somewhere on the net and need to make some alterations.
public class GenericSorter : IComparer { String sortProperty; bool sortOrder; public GenericSorter(S tring sortBy, bool asc) { sortProperty = sortBy; sortOrder = asc; } public int Compare(object x, object y) { IComparable ic1 = (IComparable)x. GetType().GetPr operty(sortProp erty).GetValue( x,null); IComparable ic2 = (IComparable)y. GetType().GetPr operty(sortProp erty).GetValue( y,null); if(sortOrder) return ic1.CompareTo(i c2); else return ic2.CompareTo(i c1); } }
It works fine if the property to sort on is in the current object, for example ID. However, if I pass sortProperty string such as "thisDepartment .ID" (in order to sort by the department that the object belings to).
Is there a way to use GetProperty to evaulate and navigate to the property in question and then get its value?
So far I have come up with the follwing solutions.
1.) Call GetProperties() and find the first property, search for another '.' if there isnt one then get the value of the string after the '.'. If there is another '.' get the propoerties for that property, and so on with recursion. Bit of a killer in terms of performance, but how else can you provide the ability to sort on any property in an object model?
2.) Scrap the generic sorter and implement a sort for each property I want to sort on that does the navigation. requires lots of code.
Any ideas would be cool.
TIA
MattC
Did this in the end:
public class GenericSorter : IComparer
{
string sortProperty;
bool sortOrder;
string type = String.Empty;
public GenericSorter(S tring sortBy, bool asc)
{
sortProperty = sortBy;
sortOrder = asc;
}
public int Compare(object x, object y)
{
type = x.ToString();
try
{
return Compare(x,y,sor tProperty);
}
catch(Exception e)
{
//throw
}
}
private int Compare(object x, object y, string property)
{
int dotindex = property.IndexO f(".");
//if this string contains a . then this is not the leaf property
if (dotindex != -1 )
{
//split the string
string thisproperty = property.Substr ing(0,dotindex) ;//get the string
for this node
string remainder = property.Substr ing(dotindex+1) ; //pass the remainder
to recursive call
return Compare( x.GetType().Get Property(thispr operty).GetValu e(x, null),
y.GetType().Get Property(thispr operty).GetValu e(y, null), remainder);
}
else//get the value of the property
{
IComparable ic1 =
(IComparable)x. GetType().GetPr operty(property ).GetValue(x,nu ll);
IComparable ic2 =
(IComparable)y. GetType().GetPr operty(property ).GetValue(y,nu ll);
if(sortOrder)
return ic1.CompareTo(i c2);
else
return ic2.CompareTo(i c1);
}
}
}
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:O4******** ******@TK2MSFTN GP14.phx.gbl... Hi,
You have to recurse, find a similar code below with that taken into account
The first if check for a "." in the property used for comparision, if its found then it meants it's part of a complex member and it simply call it recursely int Compare( object x, object y, string comparer) { if ( comparer.IndexO f( ".") != -1 ) { //split the string string[] parts = comparer.Split( new char[]{ '.'} ); return Compare( x.GetType().Get Property( parts[0]).GetValue(x, null) , y.GetType().Get Property( parts[0]).GetValue(y, null) , parts[1] ); } else { IComparable icx, icy; icx = (IComparable)x. GetType().GetPr operty( comparer).GetVa lue(x, null); icy = (IComparable)y. GetType().GetPr operty( comparer).GetVa lue(y, null);
if ( x.GetType().Get Property(compar er).PropertyTyp e == typeof(System.S tring) ) { icx = (IComparable) icx.ToString(). ToUpper(); icy = (IComparable) icy.ToString(). ToUpper(); }
if(this.sortDir ection == SortDirection.D escending) return icy.CompareTo(i cx); else return icx.CompareTo(i cy); }
}
cheers,
-- Ignacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation
"MattC" <m@m.com> wrote in message news:OP******** ******@TK2MSFTN GP12.phx.gbl... Hi,
I found this code somewhere on the net and need to make some alterations.
public class GenericSorter : IComparer { String sortProperty; bool sortOrder; public GenericSorter(S tring sortBy, bool asc) { sortProperty = sortBy; sortOrder = asc; } public int Compare(object x, object y) { IComparable ic1 = (IComparable)x. GetType().GetPr operty(sortProp erty).GetValue( x,null); IComparable ic2 = (IComparable)y. GetType().GetPr operty(sortProp erty).GetValue( y,null); if(sortOrder) return ic1.CompareTo(i c2); else return ic2.CompareTo(i c1); } }
It works fine if the property to sort on is in the current object, for example ID. However, if I pass sortProperty string such as "thisDepartment .ID" (in order to sort by the department that the object belings to).
Is there a way to use GetProperty to evaulate and navigate to the property in question and then get its value?
So far I have come up with the follwing solutions.
1.) Call GetProperties() and find the first property, search for another '.' if there isnt one then get the value of the string after the '.'. If there is another '.' get the propoerties for that property, and so on with recursion. Bit of a killer in terms of performance, but how else can you provide the ability to sort on any property in an object model?
2.) Scrap the generic sorter and implement a sort for each property I want to sort on that does the navigation. requires lots of code.
Any ideas would be cool.
TIA
MattC
This works much better, lol
object test = DataBinder.Eval (obj, "property") ;
Instead of homemade property navigation.
MattC
"MattC" <m@m.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. .. Did this in the end:
public class GenericSorter : IComparer { string sortProperty; bool sortOrder; string type = String.Empty;
public GenericSorter(S tring sortBy, bool asc) { sortProperty = sortBy; sortOrder = asc; }
public int Compare(object x, object y) { type = x.ToString(); try { return Compare(x,y,sor tProperty); } catch(Exception e) { //throw } }
private int Compare(object x, object y, string property) { int dotindex = property.IndexO f("."); //if this string contains a . then this is not the leaf property if (dotindex != -1 ) { //split the string string thisproperty = property.Substr ing(0,dotindex) ;//get the string for this node string remainder = property.Substr ing(dotindex+1) ; //pass the remainder to recursive call return Compare( x.GetType().Get Property(thispr operty).GetValu e(x, null), y.GetType().Get Property(thispr operty).GetValu e(y, null), remainder); } else//get the value of the property { IComparable ic1 = (IComparable)x. GetType().GetPr operty(property ).GetValue(x,nu ll); IComparable ic2 = (IComparable)y. GetType().GetPr operty(property ).GetValue(y,nu ll);
if(sortOrder) return ic1.CompareTo(i c2); else return ic2.CompareTo(i c1); } } } "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote in message news:O4******** ******@TK2MSFTN GP14.phx.gbl... Hi,
You have to recurse, find a similar code below with that taken into account
The first if check for a "." in the property used for comparision, if its found then it meants it's part of a complex member and it simply call it recursely int Compare( object x, object y, string comparer) { if ( comparer.IndexO f( ".") != -1 ) { //split the string string[] parts = comparer.Split( new char[]{ '.'} ); return Compare( x.GetType().Get Property( parts[0]).GetValue(x, null) , y.GetType().Get Property( parts[0]).GetValue(y, null) , parts[1] ); } else { IComparable icx, icy; icx = (IComparable)x. GetType().GetPr operty( comparer).GetVa lue(x, null); icy = (IComparable)y. GetType().GetPr operty( comparer).GetVa lue(y, null);
if ( x.GetType().Get Property(compar er).PropertyTyp e == typeof(System.S tring) ) { icx = (IComparable) icx.ToString(). ToUpper(); icy = (IComparable) icy.ToString(). ToUpper(); }
if(this.sortDir ection == SortDirection.D escending) return icy.CompareTo(i cx); else return icx.CompareTo(i cy); }
}
cheers,
-- Ignacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation
"MattC" <m@m.com> wrote in message news:OP******** ******@TK2MSFTN GP12.phx.gbl... Hi,
I found this code somewhere on the net and need to make some alterations.
public class GenericSorter : IComparer { String sortProperty; bool sortOrder; public GenericSorter(S tring sortBy, bool asc) { sortProperty = sortBy; sortOrder = asc; } public int Compare(object x, object y) { IComparable ic1 = (IComparable)x. GetType().GetPr operty(sortProp erty).GetValue( x,null); IComparable ic2 = (IComparable)y. GetType().GetPr operty(sortProp erty).GetValue( y,null); if(sortOrder) return ic1.CompareTo(i c2); else return ic2.CompareTo(i c1); } }
It works fine if the property to sort on is in the current object, for example ID. However, if I pass sortProperty string such as "thisDepartment .ID" (in order to sort by the department that the object belings to).
Is there a way to use GetProperty to evaulate and navigate to the property in question and then get its value?
So far I have come up with the follwing solutions.
1.) Call GetProperties() and find the first property, search for another '.' if there isnt one then get the value of the string after the '.'. If there is another '.' get the propoerties for that property, and so on with recursion. Bit of a killer in terms of performance, but how else can you provide the ability to sort on any property in an object model?
2.) Scrap the generic sorter and implement a sort for each property I want to sort on that does the navigation. requires lots of code.
Any ideas would be cool.
TIA
MattC
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Frazer |
last post by:
hi
could any one tell me which real life senarios reflection can be used in ?
thnx
|
by: Robert W. |
last post by:
I've almost completed building a Model-View-Controller but have run into a
snag. When an event is fired on a form control I want to automatically
updated the "connnected" property in the Model. This works fine if all of
the properties are at the top (root level) of the model but I'd like to keep
them in nested classes to organize them better.
So, for example, part of my data model looks like this (simplified) :
public class MainClass
|
by: Aaron Queenan |
last post by:
Given the classes:
class Class
{
public static implicit operator int(Class c)
{
return 0;
}
}
class Holder
|
by: Joel Reinford |
last post by:
I would like to build a class that has properties which can be accessed by
string names or index numbers in the form of MyClass.Item("LastName"). The
string names or item index values would be populated by a data-driven loop.
I need a few pointers to get me started in the right direction. I'm pretty
sure that I need a default Item property but I'm not sure how to create that
or index the other properties. A short code example is below
...
|
by: GBR |
last post by:
Hi Guys,
I have been boggling over this for the past few days. I have a class like
this:
Class A
Property Name as String
Property ID as Integer
Property Address as B
End Class
| |
by: Pritcham |
last post by:
Hi all
I've got a number of classes already developed (basic entity classes)
like the following:
Public Class Contact
Private _firstname as String
Private _age as Integer
Public Property FirstName As String
|
by: bill |
last post by:
All,
Can anyone supply an example or reference to an example of using
reflection to determine the data types contained in a nested stucture
in C#? Once I get the list of MemberInfo and determine that
MemberInfo.MemberType.ToString().Equals("NestedType"), I cannot
figure out how to drill down from that point.
TIA,
|
by: wpmccormick |
last post by:
What is the cleanest way to gain access to object methods and
properties across classes and files in the same namespace?
Example: A form object frmForm in file frmForm.cs creates obj1 defined
in file obj1.cs, which in turn creates obj2 and obj3 defined in obj2.cs
and obj3.cs, respectively.
What is the canonical way for frmForm to access obj2 and obj3? For obj3
to access frmForm?
|
by: Frank Rizzo |
last post by:
I have an app, which has a composite User Control which includes other
User Controls. The problem is that when I drop the composite user
control on the form, the nested User Controls think that they are in the
runtime mode. It seems like whenever the controls are in 2-deep in
relation to the top container, they think they are in run mode.
Is there a fix to this issue?
VS 2005, SP1.
Regards
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |