Click here to Skip to main content
14,331,326 members
Rate this:
Please Sign up or sign in to 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.
Richard Deeming 19-Dec-17 11:53am
   
Look at the inner exception, which should have more details.
Gaurav Chinchankar 20-Dec-17 1:08am
   
Sir I had uploaded a image of inner exception error, plz check below link and help me to fix the error..
https://ibb.co/j1WVjm
Richard Deeming 20-Dec-17 8:05am
   
A very blurry image, but as far as I can see, it's the problem described here:
Running .NET 2 Runtime applications under the .NET 4 Runtime[^]

You need to enable the legacy runtime activation policy in your application's config file.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100