How I can use a function from another application
-
Hai, I am having two C# applications.I need to use some functions of second application in First application. How I can do this.I need to know complete steps. I am sure somebody can show my way. Thank you, Rahul.
One option is at design time: If the other application's EXE name is called 'app.exe', copy it to 'app.exe.dll' and add it as 'Reference' to your project. Right-click on "References" in the Visual Studio's Solution Explorer, choose "Add Reference..." and "Browse..." for the 'app.exe.dll' .NET tab of the dialog. If app.exe has a class "MyClass" in the namespace "MyApp.MyNamespace" you can use it in your application with "MyApp.MyNamespace.MyClass myClass = new MyApp.MyNamespace.MyClass()", for example. /EDIT: The reason for renaming the app.exe to app.exe.dll is, that Visual Studio allows only .DLL files as file references.
-
One option is at design time: If the other application's EXE name is called 'app.exe', copy it to 'app.exe.dll' and add it as 'Reference' to your project. Right-click on "References" in the Visual Studio's Solution Explorer, choose "Add Reference..." and "Browse..." for the 'app.exe.dll' .NET tab of the dialog. If app.exe has a class "MyClass" in the namespace "MyApp.MyNamespace" you can use it in your application with "MyApp.MyNamespace.MyClass myClass = new MyApp.MyNamespace.MyClass()", for example. /EDIT: The reason for renaming the app.exe to app.exe.dll is, that Visual Studio allows only .DLL files as file references.
Another option at runtime: You can load a .NET Assembly at runtime. Assembly assembly = Assembly.LoadFile("app.exe"); Type type = assembly.GetType("MyApp.MyNamespace.MyClass"); object obj = Activator.CreateInstance(type); "type" may be -null- if the given type name wasn't found in the assembly. You can create an object without loading the assembly through the "Activator.CreateInstanceFrom" method. Just search for some articles on how using Assemblies that are loaded at runtime and/or the Activator class. I prefer the implementation at design time though.
-
Another option at runtime: You can load a .NET Assembly at runtime. Assembly assembly = Assembly.LoadFile("app.exe"); Type type = assembly.GetType("MyApp.MyNamespace.MyClass"); object obj = Activator.CreateInstance(type); "type" may be -null- if the given type name wasn't found in the assembly. You can create an object without loading the assembly through the "Activator.CreateInstanceFrom" method. Just search for some articles on how using Assemblies that are loaded at runtime and/or the Activator class. I prefer the implementation at design time though.