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
user160677
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
Marc gravell
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
queen3
source
to share
I don't think you will be able to detect element 3 using reflection.
0
Steven sudit
source
to share
Check the following members of the MethodInfo class:
- IsPublic
- IsStatic
- ReturnType
- GetParameters () method
To test if a method is recursive, I think you need more than just simple reflection.
0
Frederik gheysels
source
to share