Passing Parameters
Now with the basic form established, we can move to variations on the theme. First variation: pass parameters to the method we're calling.
Steps 1, 2 and 3 to load the DLL assembly and create a target object remain the same as the previous article:
// load the assembly
Assembly assem = Assembly.LoadFrom(@"c:\path\to\file.dll");
// create a Type object
Type typClsX = assem.GetType("Fully.Qual.ClsX",true);
// instantiate an object of that type
object oClsX = Activator.CreateInstance(typClsX);
In this article, the first method we will call requires passing one parameter of type int
. To create the proper MethodInfo object, we need to indicate that the method we're looking for takes one parameter of type int
.
The parameter signature information is passed via a System.Type
array. That is, an ordered collection of System.Type
objects lined up to match the parameters that we will pass to the method.
So we'll need to send a System.Type array of size 1 with the one array member set to System.Int32 type.
System.Type[] arrTypes = new System.Type[1];
arrTypes.SetValue(System.Int32,0);
We've gathered enough information now to properly identify the specific method that we're after:
MethodInfo miCusNm = typClsX.GetMethod("GetCustomerName",arrTypes);
If the class signature isn't recognized -- method name + parameter types and order -- an exception will be thrown with an error message stating something along the lines of "parameter mismatch".
The MethodInfo object created represents a different method from the one parameterless method in the previous article.
Next we will need to pass an integer to that method. The MethodIfno.Invoke() method, however, always takes an array of object
objects. We'll need to place our one integer into a single-member object array in order to pass it to the target method.
object[] arrParms = new object[1];
arrParms.SetValue(iCustNum,0);
string strCustName = (string) miCusNm.Invoke(oClsX,arrParms);
As you see, the size of the arrays arrTypes and arrParms must match. The Type array specifies the types and number of parameters, and the object array packages your specific parameter values that is passed to the target method.
The Snippet
// create an object of type ClsX
Assembly assem = Assembly.LoadFrom(@"c:\path\to\file.dll");
Type typClsX = assem.GetType("Fully.Qual.ClsX",true);
object oClsX = Activator.CreateInstance(typClsX);
// find the method: GetCustomerName(int)
System.Type[] arrTypes = new System.Type[1];
arrTypes.SetValue(System.Int32,0);
MethodInfo miCusNm = typClsX.GetMethod("GetCustomerName",arrTypes);
int iCustNum = 123456;
// invoke the method with our parameter value
object[] arrParms = new object[1];
arrParms.SetValue(iCustNum,0);
string strCustName = (string) miCusNm.Invoke(oClsX,arrParms);
Two Parameters
Just to reinforce this concept, I'll briefly cover passing multiple parameters. This example picks up after creating our target object.
// target method: GetCustomerNames(string state, int maxresponses);
System.Type[] arrTypes = new System.Type[2];
arrTypes.SetValue(Type.GetType("System.String"),0);
arrTypes.SetValue(Type.GetType("System.Int32"),1);
MethodInfo miCusNms = typClsX.GetMethod("GetCustomerNames",arrTypes);
string strState = "TX";
int iMaxResp = 50;
// package our parameters
object[] arrParms = new object[2];
arrParms.SetValue(strState,0);
arrParms.SetValue(iMaxResp,1);
string strCustNames = (string) miCusNms.Invoke(oClsX,arrParms);
Summary
In this article, we covered how to bind to and invoke a method at runtime through .NET Reflection. We learned how to pass parameters to the target method within that class object and cast the returned object to a string.
- Part 1: Introduction to C# Reflection
- Part 2: string s = obj.Method()
- Part 3: string s = obj.Method(strString, iNumber)
- Part 4: Setting Object Properties
- Part 5: bool b = obj.Method(iNumber, ref string strString)
- Part 6: bool b = obj.Method(iNumber, out string strString)
- Part 7: Dealing with Remote Objects
Comments