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

Access a Nested Property for a reflection based Comparer

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(String sortBy, bool asc)
{
sortProperty = sortBy;
sortOrder = asc;
}
public int Compare(object x, object y)
{
IComparable ic1 =
(IComparable)x.GetType().GetProperty(sortProperty) .GetValue(x,null);
IComparable ic2 =
(IComparable)y.GetType().GetProperty(sortProperty) .GetValue(y,null);
if(sortOrder)
return ic1.CompareTo(ic2);
else
return ic2.CompareTo(ic1);
}
}

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
Nov 16 '05 #1
3 5134
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.IndexOf( ".") != -1 )
{
//split the string
string[] parts = comparer.Split( new char[]{ '.'} );
return Compare( x.GetType().GetProperty( parts[0]).GetValue(x, null) ,
y.GetType().GetProperty( parts[0]).GetValue(y, null) , parts[1]
);
}
else
{
IComparable icx, icy;
icx =
(IComparable)x.GetType().GetProperty( comparer).GetValue(x, null);
icy =
(IComparable)y.GetType().GetProperty( comparer).GetValue(y, null);

if ( x.GetType().GetProperty(comparer).PropertyType ==
typeof(System.String) )
{
icx = (IComparable) icx.ToString().ToUpper();
icy = (IComparable) icy.ToString().ToUpper();
}

if(this.sortDirection == SortDirection.Descending)
return icy.CompareTo(icx);
else
return icx.CompareTo(icy);
}

}
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"MattC" <m@m.com> wrote in message
news:OP**************@TK2MSFTNGP12.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(String sortBy, bool asc)
{
sortProperty = sortBy;
sortOrder = asc;
}
public int Compare(object x, object y)
{
IComparable ic1 =
(IComparable)x.GetType().GetProperty(sortProperty) .GetValue(x,null);
IComparable ic2 =
(IComparable)y.GetType().GetProperty(sortProperty) .GetValue(y,null);
if(sortOrder)
return ic1.CompareTo(ic2);
else
return ic2.CompareTo(ic1);
}
}

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

Nov 16 '05 #2
Did this in the end:

public class GenericSorter : IComparer
{
string sortProperty;
bool sortOrder;
string type = String.Empty;

public GenericSorter(String sortBy, bool asc)
{
sortProperty = sortBy;
sortOrder = asc;
}

public int Compare(object x, object y)
{
type = x.ToString();
try
{
return Compare(x,y,sortProperty);
}
catch(Exception e)
{
//throw
}
}

private int Compare(object x, object y, string property)
{
int dotindex = property.IndexOf(".");
//if this string contains a . then this is not the leaf property
if (dotindex != -1 )
{
//split the string
string thisproperty = property.Substring(0,dotindex);//get the string
for this node
string remainder = property.Substring(dotindex+1); //pass the remainder
to recursive call
return Compare( x.GetType().GetProperty(thisproperty).GetValue(x, null),
y.GetType().GetProperty(thisproperty).GetValue(y, null), remainder);
}
else//get the value of the property
{
IComparable ic1 =
(IComparable)x.GetType().GetProperty(property).Get Value(x,null);
IComparable ic2 =
(IComparable)y.GetType().GetProperty(property).Get Value(y,null);

if(sortOrder)
return ic1.CompareTo(ic2);
else
return ic2.CompareTo(ic1);
}
}
}
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:O4**************@TK2MSFTNGP14.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.IndexOf( ".") != -1 )
{
//split the string
string[] parts = comparer.Split( new char[]{ '.'} );
return Compare( x.GetType().GetProperty( parts[0]).GetValue(x, null) ,
y.GetType().GetProperty( parts[0]).GetValue(y, null) , parts[1]
);
}
else
{
IComparable icx, icy;
icx =
(IComparable)x.GetType().GetProperty( comparer).GetValue(x, null);
icy =
(IComparable)y.GetType().GetProperty( comparer).GetValue(y, null);

if ( x.GetType().GetProperty(comparer).PropertyType ==
typeof(System.String) )
{
icx = (IComparable) icx.ToString().ToUpper();
icy = (IComparable) icy.ToString().ToUpper();
}

if(this.sortDirection == SortDirection.Descending)
return icy.CompareTo(icx);
else
return icx.CompareTo(icy);
}

}
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"MattC" <m@m.com> wrote in message
news:OP**************@TK2MSFTNGP12.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(String sortBy, bool asc)
{
sortProperty = sortBy;
sortOrder = asc;
}
public int Compare(object x, object y)
{
IComparable ic1 =
(IComparable)x.GetType().GetProperty(sortProperty) .GetValue(x,null);
IComparable ic2 =
(IComparable)y.GetType().GetProperty(sortProperty) .GetValue(y,null);
if(sortOrder)
return ic1.CompareTo(ic2);
else
return ic2.CompareTo(ic1);
}
}

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


Nov 16 '05 #3
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****************@TK2MSFTNGP12.phx.gbl...
Did this in the end:

public class GenericSorter : IComparer
{
string sortProperty;
bool sortOrder;
string type = String.Empty;

public GenericSorter(String sortBy, bool asc)
{
sortProperty = sortBy;
sortOrder = asc;
}

public int Compare(object x, object y)
{
type = x.ToString();
try
{
return Compare(x,y,sortProperty);
}
catch(Exception e)
{
//throw
}
}

private int Compare(object x, object y, string property)
{
int dotindex = property.IndexOf(".");
//if this string contains a . then this is not the leaf property
if (dotindex != -1 )
{
//split the string
string thisproperty = property.Substring(0,dotindex);//get the string
for this node
string remainder = property.Substring(dotindex+1); //pass the remainder
to recursive call
return Compare( x.GetType().GetProperty(thisproperty).GetValue(x,
null), y.GetType().GetProperty(thisproperty).GetValue(y, null),
remainder);
}
else//get the value of the property
{
IComparable ic1 =
(IComparable)x.GetType().GetProperty(property).Get Value(x,null);
IComparable ic2 =
(IComparable)y.GetType().GetProperty(property).Get Value(y,null);

if(sortOrder)
return ic1.CompareTo(ic2);
else
return ic2.CompareTo(ic1);
}
}
}
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote in message news:O4**************@TK2MSFTNGP14.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.IndexOf( ".") != -1 )
{
//split the string
string[] parts = comparer.Split( new char[]{ '.'} );
return Compare( x.GetType().GetProperty( parts[0]).GetValue(x, null) ,
y.GetType().GetProperty( parts[0]).GetValue(y, null) , parts[1]
);
}
else
{
IComparable icx, icy;
icx =
(IComparable)x.GetType().GetProperty( comparer).GetValue(x, null);
icy =
(IComparable)y.GetType().GetProperty( comparer).GetValue(y, null);

if ( x.GetType().GetProperty(comparer).PropertyType ==
typeof(System.String) )
{
icx = (IComparable) icx.ToString().ToUpper();
icy = (IComparable) icy.ToString().ToUpper();
}

if(this.sortDirection == SortDirection.Descending)
return icy.CompareTo(icx);
else
return icx.CompareTo(icy);
}

}
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"MattC" <m@m.com> wrote in message
news:OP**************@TK2MSFTNGP12.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(String sortBy, bool asc)
{
sortProperty = sortBy;
sortOrder = asc;
}
public int Compare(object x, object y)
{
IComparable ic1 =
(IComparable)x.GetType().GetProperty(sortProperty) .GetValue(x,null);
IComparable ic2 =
(IComparable)y.GetType().GetProperty(sortProperty) .GetValue(y,null);
if(sortOrder)
return ic1.CompareTo(ic2);
else
return ic2.CompareTo(ic1);
}
}

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



Nov 16 '05 #4

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

Similar topics

5
by: Frazer | last post by:
hi could any one tell me which real life senarios reflection can be used in ? thnx
8
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. ...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
8
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...
0
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
4
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...
2
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...
5
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
isladogs
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...

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.