Open Menu dzosoft
Close Menu dzosoft

   ALL ABOUT COMPUTER SCIENCE AND TECHNOLOGY


                             




A MALE SOLUTION TO PROSTATE PROBLEMS!

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);
}

} }


It might also interest you


C# Get Started

Using ChatGPT in C#

Quick overview of the differences between C# and Visual Basic .NET

How to create a local MySQL connection in C#


Leave comment
          

Save nickname and email in this browser for the next time.



Loading...     
close publicity
BUILD THE MASS IN A FEW MONTHS THAT OTHERS BUILD IN YEARS!