0

I am trying to call a method in winForms application from a command line application using reflection. This method launches the application with loaded configuration.

Assembly exeApp = Assebmly.LoadFile(exeAppPath);
Type classType = exeApp.GetType(nameSpace);
object obj = classType.GetConstructor(Type.EmptyTypes).Invoke(new object[] { });
MethodInfo exeMethod classType.GetMethod(MethodName);

exemethod.Invoke(obj, args);

Using this, I am able to call the method, but I get following exception for all referenced assemblies in my target exe:

Exception thrown: 'System.IO.FileNotFoundException' in winFormsApp.exe
Additional information: Could not load file or assembly 'refAssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified

If I add references to all those assemblies in my command line app, it works correctly.

The problem here is, the exe references keeps on changing/updating as per releases, and it makes difficult to update/release commandLine app everytime.

Is there any way I can make it work without adding the references in commandLine app ?

Sinatr
  • 20,892
  • 15
  • 90
  • 319
TK_Dev
  • 11
  • 1
  • You can manully resolve the assembly - https://stackoverflow.com/questions/5260404/resolve-assembly-references-from-another-folder – Rand Random Oct 22 '20 at 12:05
  • Error thrown is: Exception thrown: 'System.IO.FileNotFoundException' in winFormsApp.exe Additional information: Could not load file or assembly 'refAssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified. – TK_Dev Oct 22 '20 at 12:56
  • Does this answer your question? [Is there a way to force all referenced assemblies to be loaded into the app domain?](https://stackoverflow.com/questions/2384592/is-there-a-way-to-force-all-referenced-assemblies-to-be-loaded-into-the-app-doma) – Sinatr Oct 22 '20 at 13:30
  • Using Assembly.LoadFile() is wrong 99.9% of the time. Use Load() or LoadFrom() instead so it has a shot at finding dependent assemblies. – Hans Passant Oct 22 '20 at 15:32

0 Answers0