Open Menu dzosoft
Close Menu dzosoft

   TOUT SUR L'INFORMATIQUE ET LA TECHNOLOGIE


                             





Nord VPN

Publish perfectly-optimized content in 1-click



 
 
 

How To Execute A .NET Assembly Inside A C# Program

 

It's very easy to call a .NET assembly (a compiled C# program – exe or DLL) within another one.

 To see how it works, I created a simple roject that prints hello world

using System;

namespace hello { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!");
} } }

 Now I created another project to execute the .Net assebly of the previous project

using System;
using System.IO;
using System.Reflection;

namespace LoadAssembly { class Program { static void Main(string[] args) {
Byte[] bytes = File.ReadAllBytes(@"C:/Users/dzosoft/source/repos/Hello/bin/Debug/Hello.exe");

Assembly assembly = Assembly.Load(bytes); MethodInfo method = assembly.EntryPoint; string[] param = new string[] { "hi" }; object[] parameters = new[] { param };
object execute = method.Invoke(null, parameters);
} } }


 That will work and print out “hello world!”. This works for DLLs too, change the file to have this instead and build it again.

using System;
using System.IO;
using System.Reflection;

namespace LoadAssembly { class Program { static void Main(string[] args) {
Byte[] bytes = File.ReadAllBytes(@"/Users/guru/hello.dll");

Assembly assembly = Assembly.Load(bytes); MethodInfo method = assembly.EntryPoint; string[] param = new string[] { "hi" }; object[] parameters = new[] { param };
object execute = method.Invoke(null,parameters);
}

} }


También te puede interesar


C# Premiers pas

Utiliser ChatGPT en C#

Aperçu rapide des différences entre C# et Visual Basic .NET

Comment créer une connexion MySQL locale en C#


Leave comment
          

Enregistrez le pseudo et l'e-mail dans ce navigateur pour la prochaine fois.



Cargando...     

Nord VPN