Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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;
    }
}


What I have tried:

Tried different techniques...failed!
Posted
Updated 18-Dec-17 20:02pm
Comments
Karthik_Mahalingam 19-Dec-17 2:19am    
add a break here
  classType = types[0];
  break;
Gaurav Chinchankar 19-Dec-17 5:13am    
Sir, Issue is not at that part..

dynamic classInstance = (dynamic) Activator.CreateInstance(classType)
/*When you call above method to create instance of class..the error occurs*/
Karthik_Mahalingam 20-Dec-17 0:18am    
are you getting null in
Type classType = LoadDll();
Ramza360 19-Dec-17 9:39am    
You should add a breakpoint to ensure the classType is not null at that point. Also if your type has no default constructor, you must provided the constructor arguments.
Gaurav Chinchankar 20-Dec-17 0:53am    
Sir, I checked using break-point. The class type is never null, the dll which doesn't have C++ COM supporting dll's works properly. This errors only occurs for supporting C++ dll's.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900