HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Best way to locate Find on List<T> with reflection

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
reflectionwithwayfindlocatelistbest

Problem

I am locating the find method of List in C# with the single Expression type parameter

I am using the following ( where T is a List, in my case a SubSonic IActiveRecord )

MethodInfo info = typeof(T).GetMethods(BindingFlags.Static | BindingFlags.Public)
                             .FirstOrDefault(m => m.Name == "Find" && m.GetParameters().Count() == 1);


This works perfectly well, but feels clunky (especially the parameter count part) and I'm not sure how future proof it is.

Solution

When searching for methods using reflection I found that the most future-proof way is by searching exactly for the method, so not only the Count() is correct but I would also have checked both what is this parameter type and what is the return type of the function.

Context

StackExchange Code Review Q#1244, answer score: 6

Revisions (0)

No revisions yet.