Reflecting Parameters

I got a question last week from a gentleman asking how to tell what parameters were available for a particular method.  This is useful when you know that a method will be available on a class you are calling, but the method could have any parameters, or when you have used reflection to get the list of methods in your class, and need to pull out the parameter list for each.

The solution is actually quite simple:

First, let’s use the assumption that we know the method name.  To get the parameters once you have the method, use this code:

Object o = myAssembly.CreateInstance("ClassLibrary1.Class1");

Type myType = myAssembly.GetType("ClassLibrary1.Class1");
MethodInfo mi = myType.GetMethod("Go");
ParameterInfo[] arguements = mi.GetParameters();

 

Given any method, Go(), this will give us the list of parameters for this method.

When iterating through the members, there is a small catch.  If you are iterating through your members generally (methods, and properties) than you’ll need to first detect if the member is a method using,

if (mi.MemberType==MemberTypes.Method)

and then cast the MemberInfo object to a MethodInfo so that you can call GetParameters().  Forget this step and you’ll spend the rest of the day trying to figure out why your code won’t compile.


Other Related Items:

Wolf Reflection Design Decorative Protector Skin Decal Sticker for Nintendo DS LiteWolf Reflection Design Decorative Protector Skin Decal Sticker for Nintendo DS LiteScratch resistant skin sticker helps to protect your Nintendo DS Lite while making an impression. Self-adhesive plastic-coated skins cover the are cus... Read More >
goLINK OBD-II to iPhone/Touch by GoPoint TechnologygoLINK OBD-II to iPhone/Touch by GoPoint TechnologyOur goLINK protocol converter, an Apple "Works With iPhone" product, will directly connect your vehicles engine On Board Diagnostics (OBD) data to you... Read More >
Hanes Silk Reflections Silky Sheer Control Top - 717Hanes Silk Reflections Silky Sheer Control Top - 717This Hanes Silk Reflections Silky Sheer Control Top features an amazing fit and feel!

Related Post

Comments are closed.

DotNetNuke Sponsor

 

Most Valuable Blogger
Sponsor