## Passing "ref" Parameters

So far we've made conventional reflection calls. Things get a bit sticky, however, when the method we want to invoke contains a ref parameter. For example, a method with the signature:

bool CheckCustName(int iCust, ref string strName);


Reference parameters are not copied into the receiving method, but referenced from the caller's memory area. Creating the object is not a problem if you've been reading this series: This part was covered in depth in Part 2:

Assembly assem = Assembly.LoadFrom(@"c:\path\to\file.dll");
Type typClsX = assem.GetType("Fully.Qual.ClsX",true);
object oClsX = Activator.CreateInstance(typClsX);


We now have our object and we need to find the CheckCustName method shown above. The method used in Part 3 to describe the order and number of parameters within the targeted method are still used.

System.Type[] arrTypes = new System.Type[2];
arrTypes.SetValue(Type.GetType("System.Int32"),0);
arrTypes.SetValue(Type.GetType("System.String&"),1);
MethodInfo miCusNm = typClsX.GetMethod("CheckCustName",arrTypes);


The method is found because we indicated in the arrTypes array that the second parameter was a reference to a string. This was accomplished by adding an ampersand to the end of the type string. That is, "System.String&". Now with method located, we continue along invoking the method as has already been shown...

int iCustNum = 12345;
string strCustName = "Global Conglomerate Inc";

// package our parameters
object[] arrParms = new object[2];
arrParms.SetValue(iCustNum,0);
arrParms.SetValue(strCustName,1);
bool success = (bool) miCusNm.Invoke(oClsX,arrParms);


## The Snippet

// get an instansiation of type oClsx
Type typClsX = assem.GetType("Fully.Qual.ClsX",true);
object oClsX = Activator.CreateInstance(typClsX);

// locate method CheckCustName(int, ref string)
System.Type[] arrTypes = new System.Type[2];
arrTypes.SetValue(Type.GetType("System.Int32"),0);
arrTypes.SetValue(Type.GetType("System.String&"),1);
MethodInfo miCusNm = typClsX.GetMethod("CheckCustName",arrTypes);

int iCustNum = 12345;
string strCustName = "Global Conglomerate Inc";

// package our parameters and invoke method
object[] arrParms = new object[2];
arrParms.SetValue(iCustNum,0);
arrParms.SetValue(strCustName,1);
bool success = (bool) miCusNm.Invoke(oClsX,arrParms);


## Summary

In this article, we learned how invoke a reflected method that uses a ref "reference" parameter.