C# - Exception when C++ COM dll's used in reflection

Dec 20 2017 1:12 AM
Whenever my supporting COM dll's is called through reflection technique in C#, It gives me "Exception has been thrown by the target of an invocation."
 
My Code:
 
public class ExecuteAssembly { IAssembly objIAssembly = new Xml_Operations.XmlRetrive();
public void StartExecution()
{
try
{
Type classType = LoadDll();
// create instance of class Calculator
//object classInstance = Activator.CreateInstance(classType);
dynamic classInstance = (dynamic) Activator.CreateInstance(classType);  <-- ERROR GETS FIRED
classInstance.Visible = true;
// get info about method: public
MethodInfo methodCall = classType.GetMethod(objIAssembly.MethodName);
ParameterInfo[] parameters = methodCall.GetParameters();
objIAssembly.MethodOutput = methodCall.Invoke(classInstance, objIAssembly.ParametersArray);
Xml_Operations.XmlStore.OutputToXmlFile(objIAssembly);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private Type LoadDll()
{
string[] dirSplit=objIAssembly.DllPath.Split('\\');
string dllName = dirSplit[dirSplit.Length - 1].Substring(0, dirSplit[dirSplit.Length - 1].Length - 4); ;
int count = dirSplit[dirSplit.Length - 1].Count();
string dirPath = objIAssembly.DllPath.Substring(0, (objIAssembly.DllPath.Length - count));
var files = Directory.GetFiles(dirPath ,"*.dll");
Type[] types;
Type classType = null;
foreach (var file in files)
{
try
{
types = Assembly.LoadFrom(file).GetTypes();
if (types[0].AssemblyQualifiedName.Contains(dllName))
classType = types[0];
}
catch
{
continue; // Can't load as .NET assembly, so ignore
}
}
return classType;
}
}

Answers (3)