TipsOnLips.net

Your .net tips and tricks source

About the author

Author Name is someone.
E-mail me Send mail

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010


Invoke a method via reflection in C#

To invoke a method via reflection including passing parameters and getting a return value, do the following:

First, create a new class library project called ReflectionTut as show in Fig (1) below: [more]

 

 

                                                                           Fig (1) 

 

Then create a class called Utility and insert the code as in Fig (2);

 

                                           Fig (2)

 

Then create a new windows form project and insert the code in Fig (3) behind the form:

 

                                                                                    Fig (3)

The code in fig (3) first loads the class library assembly ReflectionTut.dll via reflection and because we know that we have only one class (or one type), therefore we are choosing the first type via

 Then we create a new instance of the class Utility:

 Then create an object collection for the method (GetTotal) parameters:

 

 

At last we invoke the GetTotal method via reflection and retrieve the return value which is showing in debug mode:

 

Source code: InvokeMethodReflection.rar (38.72 kb) 

 

 


Posted by developer on Wednesday, March 19, 2008 6:02 AM
Permalink | Comments (0) | Post RSSRSS comment feed
Comments are closed