473,498 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Method info discovery using Reflection, variable argument list

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
}
}
}
}
}

Nov 16 '05 #1
4 7786
JH <jo**@spamme.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
JH

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
JH <jo**@spamme.com> wrote:
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.


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.
Nov 16 '05 #3
JH <jo**@spamme.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
JH

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
JH <jo**@spamme.com> wrote:
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.


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.
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2784
by: Eric A. Forgy | last post by:
Hello, I am just learning Java and am trying to write a method that does something like //=========================================== public Static List find(double array,double val,String...
3
6285
by: Manfred Braun | last post by:
Hi All, I am able to invoke some methods from an instance of a loaded assembly, but I am not able to invoke the loaded assemblie's "Main" method. I try it this way: Assembly assembly =...
6
17112
by: Paul Welter | last post by:
I'm trying to get a method using Type.GetMethod. There are two methods with that same name, one is a standard method, the other is a Generic method. How do I get the Generic method? Is there a...
11
28106
by: placid | last post by:
Hi all, Is it possible to be able to do the following in Python? class Test: def __init__(self): pass def puts(self, str): print str
3
1956
by: =?Utf-8?B?QWRhbSBN?= | last post by:
Hi all, What is the syntax for a method that accepts a list of generic lists? For example, I have a List that contains the following lists: List<Car> List<Boat> List<Plane>
4
6801
by: =?Utf-8?B?QWJoaQ==?= | last post by:
I am using Reflection to invoke methods dynamically. I have got a special requirement where I need to pass a value to method by setting the custom method attribute. As I cannot change the...
3
2237
by: Anders Borum | last post by:
Hi I need to invoke a generic method determined at runtime. The method has two arguments, a string and a generic type that is constrained to a struct: public void Add<T>(string key, T value)...
0
2322
by: =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?= | last post by:
"Anders Borum" wrote: Hi Anders, I'm afraid the GetMethod() does not currently support filtering on generic parameters so you will have to loop through the existing methods using...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
290
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.