Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reflection InvokeMethod and returning a class

Status
Not open for further replies.

sjc

IS-IT--Management
Jun 7, 2001
41
GB
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)
 
Fixed it by making the returned class serializable
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top