Hi All,
I'm trying to use reflection (InvokeMethod()) on one of my own external assemblies to run one of it's methods. I can get everything to work fine if the method returns a simple type like an Int or string but the method in question returns a class which is a wrapper around an ArrayList.
My code as follows (I've stripped out quite a bit of the fluff to try and make it easier to read):
Assembly assembly = Assembly.LoadFrom(filename);
Type type = new assembly.GetTypes()
object ibaseObject = Activator.CreateInstance(type);
object result;
// this statement fails with: System.Reflection.TargetInvocationException
result = type.InvokeMember("MyMethod", BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod, null, ibaseObject, new Object[0]);
The class being called is like this:
namespace TestAddin
{
public class Tester: INETCLAddin
{
public Tester()
{
}
// if this method returns an int it will work fine
public FunctionList MyMethod()
{
FunctionList fl = new FunctionList();
AFunction funca = new AFunction "TestAddin", "SomeFunction"
fl.Add(funca);
return fl;
}
}
}
and the class being returned is defined as:
public class AFunction
{
public string assemblyName;
public string cmdLine;
public AFunction(string assemblyname, string cmdline)
{
assemblyName = assemblyname;
cmdLine = cmdline;
}
}
public class FunctionList
{
private ArrayList FList;
public int Count {get {return FList.Count;} }
public void Add(AFunction function)
{
FList.Add(function);
}
public AFunction Items(int index)
{
return (AFunction)FList[index];
}
}
Thanks (sorry for long post)
I'm trying to use reflection (InvokeMethod()) on one of my own external assemblies to run one of it's methods. I can get everything to work fine if the method returns a simple type like an Int or string but the method in question returns a class which is a wrapper around an ArrayList.
My code as follows (I've stripped out quite a bit of the fluff to try and make it easier to read):
Assembly assembly = Assembly.LoadFrom(filename);
Type type = new assembly.GetTypes()
object ibaseObject = Activator.CreateInstance(type);
object result;
// this statement fails with: System.Reflection.TargetInvocationException
result = type.InvokeMember("MyMethod", BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod, null, ibaseObject, new Object[0]);
The class being called is like this:
namespace TestAddin
{
public class Tester: INETCLAddin
{
public Tester()
{
}
// if this method returns an int it will work fine
public FunctionList MyMethod()
{
FunctionList fl = new FunctionList();
AFunction funca = new AFunction "TestAddin", "SomeFunction"
fl.Add(funca);
return fl;
}
}
}
and the class being returned is defined as:
public class AFunction
{
public string assemblyName;
public string cmdLine;
public AFunction(string assemblyname, string cmdline)
{
assemblyName = assemblyname;
cmdLine = cmdline;
}
}
public class FunctionList
{
private ArrayList FList;
public int Count {get {return FList.Count;} }
public void Add(AFunction function)
{
FList.Add(function);
}
public AFunction Items(int index)
{
return (AFunction)FList[index];
}
}
Thanks (sorry for long post)