Method info discovery using Reflection, variable argument list


J

JH

Fairly new to c# so please forgive any simple oversights.

I'm developing a console app that loads dll assemblies at run time. The app
scans a "plugin" directory for these dlls. Each dll is loaded into an
Assembly object, the types in the assembly are extracted and the methods on
each type are extracted. If a method matches certain criteria (i.e. public
static, a specific argument list(string, string, boolean)) then i store
details about the method. Any method "matches" that are found will be
invoked at a later time.

My problem is that while most of these "target" methods will have the same
argument list (string,string,boolean) some may have additonal parameters
e.g. string, string, boolean, int, int. I also need to store these new
methods and if possible i will need to get some information on the new
parameters to identify them to the user i.e. parameter name.

Is what i describe about the variable arguments possible to do? if so how?
Thanks in advance for any help provided.

For reference here is a snippet of the code i currently have:

foreach (Type t in types)
{
if (t.IsClass)
{
// Find any target methods for this class and add references to the
hashtables
MethodInfo[] methodsList = t.GetMethods(BindingFlags.Public |
BindingFlags.Static | BindingFlags.DeclaredOnly);
foreach(MethodInfo mtdInfo in methodsList)
{
ParameterInfo[] paramTypes = mtdInfo.GetParameters();
if (paramTypes.Length == 3)
{
if ((paramTypes[0].ParameterType == Type.GetType("System.string"))&&
(paramTypes[1].ParameterType == Type.GetType("System.string"))&&
(paramTypes[2].ParameterType == Type.GetType("System.Boolean")))
{
// Add method name + assembly to hashtables
}
}
}
}
}
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

JH said:
Fairly new to c# so please forgive any simple oversights.

I'm developing a console app that loads dll assemblies at run time. The app
scans a "plugin" directory for these dlls. Each dll is loaded into an
Assembly object, the types in the assembly are extracted and the methods on
each type are extracted. If a method matches certain criteria (i.e. public
static, a specific argument list(string, string, boolean)) then i store
details about the method. Any method "matches" that are found will be
invoked at a later time.

My problem is that while most of these "target" methods will have the same
argument list (string,string,boolean) some may have additonal parameters
e.g. string, string, boolean, int, int. I also need to store these new
methods and if possible i will need to get some information on the new
parameters to identify them to the user i.e. parameter name.

Is what i describe about the variable arguments possible to do? if so how?
Thanks in advance for any help provided.

Yes, all of that is possible. Which part are you having trouble with?
The code you gave showed that you know about ParameterInfo - that has
the type and the name.

Btw, I would suggest using typeof(...) rather than Type.GetType(...)
for types you know about in advance.
 
J

JH

Jon Skeet said:
Yes, all of that is possible. Which part are you having trouble with?
The code you gave showed that you know about ParameterInfo - that has
the type and the name.

Btw, I would suggest using typeof(...) rather than Type.GetType(...)
for types you know about in advance.

Cheers Jon,
I think i know what to do now, i just had some mental block about variable
parameter lists (memories from my C days!).
If i check for methods with a minimum of three parameters that match the
spec then i store the method name. I will also store the ParamTypes array
with the method name, so at a later stage i can itterate through the array
and present the user with the names of any additional parameters. Then i can
invoke the method. Seems reasonable enough.
 
J

Jon Skeet [C# MVP]

JH said:
I think i know what to do now, i just had some mental block about variable
parameter lists (memories from my C days!).
If i check for methods with a minimum of three parameters that match the
spec then i store the method name. I will also store the ParamTypes array
with the method name, so at a later stage i can itterate through the array
and present the user with the names of any additional parameters. Then i can
invoke the method. Seems reasonable enough.

Why not just store the MethodInfo? That's what you'll need to Invoke,
and that has the name and the parameters associated with it anyway.
 
Ad

Advertisements

J

JH

Jon Skeet said:
Why not just store the MethodInfo? That's what you'll need to Invoke,
and that has the name and the parameters associated with it anyway.

Yep thats the way alright! I was creating a simple class to store all the
method info, only a few minutes later did it click that all this info is got
from the MethodInfo, so best to store that instead. Reinventing the wheel
etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top