0

All, I have a number of C# DLLs that I want to call from my application at runtime using System.Reflection. The core code I use is something like

DLL = Assembly.LoadFrom(Path.GetFullPath(strDllName));
classType = DLL.GetType(String.Format("{0}.{0}", strNameSpace, strClassName));
if (classType != null)
{
    classInstance = Activator.CreateInstance(classType);
    MethodInfo methodInfo = classType.GetMethod(strMethodName);
    if (methodInfo != null)
    {
        object result = null;
        result = methodInfo.Invoke(classInstance, parameters);
        return Convert.ToBoolean(result);
    }
}

I would like to know how I can pass in the array of parameters to the DLL as ref so that I can extract information from what happened inside the DLL. A clear portrayal of what I want (but of course will not compile) would be

result = methodInfo.Invoke(classInstance, ref parameters);

How can I achieve this?

MoonKnight
  • 23,214
  • 40
  • 145
  • 277
  • May be this will be helpful: http://stackoverflow.com/questions/1551761/ref-parameters-and-reflection – Dennis May 28 '12 at 09:20

1 Answers1

2

Changes to ref parameters are reflected in the array that you pass into MethodInfo.Invoke. You just use:

object[] parameters = ...;
result = methodInfo.Invoke(classInstance, parameters);
// Now examine parameters...

Note that if the parameter in question is a parameter array (as per your title), you need to wrap that in another level of arrayness:

object[] parameters = { new object[] { "first", "second" } };

As far as the CLR is concerned, it's just a single parameter.

If this doesn't help, please show a short but complete example - you don't need to use a separate DLL to demonstrate, just a console app with a Main method and a method being called by reflection should be fine.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks very much Jon. I was previously doing `result = methodInfo.Invoke(classInst, new object[] { dllParams });` but was not aware that this is what this I was doing - amature. I will try this now and get back to you. Thanks again for your time. – MoonKnight May 28 '12 at 09:36