Exploring Information "Method" using Reflection?
My intention is to investigate the "Methods" Type using reflection to check the following:
-
Methods must be instance methods and public.
-
Takes a "params" parameter and is void in nature.
-
The method does not make a recursive call.
I started like:
static void ProcessMethodInfo(Type t)
{
MethodInfo[] info = t.GetMethods();
foreach (MethodInfo mi in info)
{
// How to check the conditions here ?
}
}
But I don't know how to move on. Do you need help.
+2
source to share
4 answers
It's good if by 3 you understand that the method being tested must be non-recursive; then it's a pain - you will need to parse the IL. But for the rest;
Type type = ...
var qry = from method in type.GetMethods(
BindingFlags.Instance | BindingFlags.Public)
where method.ReturnType == typeof(void)
let parameters = method.GetParameters()
where parameters.Length == 1
&& parameters[0].ParameterType.IsArray
&& Attribute.IsDefined(parameters[0], typeof(ParamArrayAttribute))
select method;
foreach (var method in qry)
{
Console.WriteLine(method.Name);
}
+3
source to share
- mi.IsStatic etc. - read the help
- Determining if a parameter is using "parameters" using reflection in C #?
- http://www.codeproject.com/KB/cs/sdilreader.aspx
ALL: use google;)
+1
source to share