JP,
strings - although reference types - behave a little differently. If you
call the method directly also, you'll notice that the parameter you pass in
does not change.
TestMethods tm = new TestMethods();
string param = "parameter1 value";
tm.TestInvoke(param);
Console.WriteLine(param);
This will still print "parameter1 value" to the console. You'll have to
define the parameter as a ref parameter and make the call accordingly as
well.
public string TestInvoke(ref string parameter1)
{
parameter1 = "Changed parameter1 value";
return parameter1;
}
TestMethods tm = new TestMethods();
string param = "parameter1 value";
tm.TestInvoke(ref param);
Console.WriteLine(param);
This will now print "Changed parameter1 value" to the console. So, you'll
have to change your TestInvoke method to take in a ref string parameter.
Now regarding MethodInfo.Invoke, can you not use the GetMethod that just
takes in the method name? Here's how:
public void Invoke()
{
// Type[] _parameterTypes = new Type[_parameters.Length];
// for(int _index = 0; _index < _parameters.Length; _index++)
// {_parameterTypes[_index] = _parameters[_index].GetType();}
System.Reflection.MethodInfo _method =
_handler.GetType().GetMethod(_methodName);
if (_method == null)
{
//throw new
ConfigurationException(Resources.ResourceManager[Resources.MessageKey.Method
NameNotFound, _methodName, _handler]);
}
string _result = (string)_method.Invoke(_handler, _parameters);
Console.WriteLine(_parameters.GetValue(0).ToString());
}
If you need to use the parameters anyway, you will need to indicate that the
parameter is a ref parameter when getting the Type of the parameter. Here's
how:
public void Invoke()
{
Type[] _parameterTypes = new Type[_parameters.Length];
for(int _index = 0; _index < _parameters.Length; _index++)
{_parameterTypes[_index] =
Type.GetType(_parameters[_index].GetType().FullName + "&");}
System.Reflection.MethodInfo _method =
_handler.GetType().GetMethod(_methodName, _parameterTypes);
if (_method == null)
{
//throw new
ConfigurationException(Resources.ResourceManager[Resources.MessageKey.Method
NameNotFound, _methodName, _handler]);
}
string _result = (string)_method.Invoke(_handler, _parameters);
Console.WriteLine(_parameters.GetValue(0).ToString());
}
hope that helps..
Imran.
JP Wrye said:
Sure,
//The Method to be invoked.
public class TestMethods
{
public TestMethods()
{}
public string TestInvoke(string parameter1)
{
parameter1 = "Changed parameter1 value";
return parameter1;
}
}
//The class to use InvocationHandler.
public class InvokeMethods
{
public void Invoke()
{
TestMethods _testMethods = new TestMethods()
InvocationHandler _invocationHandler = new
InvocationHandler(_testMethods,
"TestInvoke",
new object[] {"parameter1 value"})
}
}
public class InvocationHandler
{
private object _handler;
private string _methodName;
private object[] _parameters;
#region Constructor
public InvocationHandler(object handler, string methodName, Array
parameters)
{
_handler = handler;
_methodName = methodName;
_parameters = (object[])parameters;
}
#endregion
#region Method
public void Invoke()
{
Type[] _parameterTypes = new Type[_parameters.Length];
for(int _index = 0; _index < _parameters.Length; _index++)
{_parameterTypes[_index] = _parameters[_index].GetType();}
MethodInfo _method = _handler.GetType().GetMethod(_methodName,
_parameterTypes);
if (_method == null)
{throw new
ConfigurationException(Resources.ResourceManager[Resources.MessageKey.Method
NameNotFound,
_methodName, _handler]);}
_result = _method.Invoke(_handler, _parameters);
}
#endregion
}
I get the "_result" of the method("Changed parameter1 value"), but if I
index into the "_parameter" array "_parameter[0]", the value isn't updated.
It's still "parameter1 value".
Thanks,
JP
Imran Koradia said:
Could you post the code? That might help figuring out what's going on..
Imran.
in
the