Dynamic Loading and Calling Open Method


#1

For a client which requires Xcopy deployment of the program used along with Aspose, we need to load the Aspose DLL dynamically. Developers who regularly need to compile the project will not have access to Aspose.

To satisfy this requirement, we plan to load Aspose.Excel.dll dynamically.

I'm having a problem however calling the Open method using InvokeMember. Can you look at the code below and tell me if you can see why InvokeMember doesn't find the method? The instance seems to load fine.

using System;
using System.Reflection;

class DynamicLoadForXCopyDeploy
{
public static void Main()
{
Assembly assembly;

int callNumber = 0;
Type typeExcel = null;;

// fully qualified name (public key obtained using sn.exe -t)
assembly = Assembly.Load("Aspose.Excel, 3.3.3.0, , a1e5b9f49b2541db");
//assembly = Assembly.Load("Aspose.Excel, 3.3.0.0, , 26a860166541c6a2");
Type[] arrTypes = assembly.GetTypes();
foreach (Type t in arrTypes)
{
MethodInfo[] arrMI =
t.GetMethods(BindingFlags.Public|
BindingFlags.Instance|
BindingFlags.DeclaredOnly);

foreach (MethodInfo mi in arrMI)
{
ParameterInfo[] arrPI =
mi.GetParameters();
foreach (ParameterInfo pi in arrPI)
{
// search for method in dynamically loaded assembly
callNumber++;
Console.WriteLine(
"Type," + t.Name +
" Method," + mi.Name +
" ParameterInfo," + pi.Name
&nbspWink;

if ((typeExcel==null) && (t.Name == "Excel") && (mi.Name=="Open") && (pi.Name == "fileName"))
{
typeExcel = t;
}

}
}
}

if (typeExcel == null)
{}
else
{
int param = 1;
Object[] objCtorArgs = new Object[] {param};
object MyClass = Activator.CreateInstance(typeExcel, null);

string param2 = "C:\\aaron\\testfiles\\InboundFileExample.xls";
object[] args = new object[] {param2};


//---- This part can't find the Open method:

//object result;
//result = typeExcel.InvokeMember ("Open",
// BindingFlags.Public | BindingFlags.InvokeMethod,
// null, MyClass, args);
}
}
}


#2

Please try:

object result;
result = typeExcel.InvokeMember (“Open”,
BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance,
null, MyClass, args);


#3

It worked great. Thanks so much Laurence!