| |
|
Order by with LINQ to Reflection using MetadataToken and Lambda in C#
|
| |
|
|
| |
| DISCLAIMER: Most if not all of the methods used with Reflection do not
return their result in neither alphabetical or declaration order. This is clearly
stated within the MSDN library remarks for the reflection methods. If you can avoid
designing based on an expected order of results from any Reflection method, that
would be best practice. |
| |
| I had built a very nice program that used reflection where I had gone
against the suggestion not to depend on the order or results from methods. It was
working fine for quit some time. It stopped working. Initially, the order of the
results from the GetTypes() and GetProperties() methods were returining the result
in order of declaration and I have not idea what changed to make it stop working. |
| |
| So I searched the internet, like always, and didn't find a lot about
this topic. However, I found enough to come up with this solution which works for me.
The below is the original LINQ to Reflection that worked for me initially and then
stopped working at some point. |
| |
var SCTypes = from type in assembly.GetTypes()
where type.IsPublic
from properties in type.GetProperties()
group properties.ToString() by type.ToString();
|
| |
| Here is the code which I now run to ordered the results from a
Reflection method in declaration order. In this test case, both LINQ queries
return the same results. However, in my actual implementation where the order
was needed, the below ordered it correctly. Please read DISCLAIMER again.
There is still no guarentee that another issue will popup in the future. |
| |
var SCTypes = from type in assembly.GetTypes()
.OrderBy(t => t.MetadataToken)
where type.IsPublic
from properties in type.GetProperties()
.OrderBy(p => p.MetadataToken)
group properties.ToString()
by type.ToString();
|
| |
| You see I have simply added an OrderBy method to the GetTypes() method.
The t represents the results of the GetTypes() method and then it "goes to" the
t.MetadataToken expression. |
| |
| Top display the results you just need to place it within a
foreach statement. |
| |
foreach (var SCMethods in SCTypes)
{
Console.WriteLine("Type: {0}", SCMethods.Key);
foreach (var method in SCMethods)
{
Console.WriteLine(" Property: {0}", method);
}
}
|
| |
| Like I mentioned, both of the above LINQ queries, in this example,
returned the same thing for me. However, if you experience unordered results from
a Reflection method, you can give it a try and see if it works for you. If you
download the code, you need to put the SimpleClass.dll into your projects bin\Debug
directory.
|
| |
| Download the source |
|
|
| |
|
|
| |
|
|
| |
| |
| Posts: 113 |
| Comments:
86 |
| Fundamentals:
16 |
| |
 |
| |
| |
|
| |
 |
| |
 |
| |
 |
| |
|
| 2011 December (2) |
| 2011 November (6) |
| 2011 October (7) |
| 2011 September (7) |
| 2011 August (9) |
| 2011 July (9) |
| 2011 June (8) |
| 2011 May (9) |
| 2011 April (7) |
| 2011 March (9) |
| 2011 February (8) |
| 2011 January (8) |
| 2010 December (7) |
| 2010 November (8) |
| 2010 October (4) |
| |
| |
|
| |
|
|
| |
| |
|
The sample code on this website is provided to illustrate a concept and should not be used in
applications or Web sites without proper professional consultation, as it may not illustrate
the safest coding practices. I assume no liability for incidental or consequential damages
should the sample code be used for purposes other than as intended.
|
| |
|
| | | |