using System;
namespace hello
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
|
Ahora creé otro proyecto para ejecutar el ensamblaje .Net del proyecto anterior. |
|
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);
}
}
}
|
|
Eso funcionará e imprimirá "¡hola mundo!". Esto también funciona para archivos DLL, cambie el archivo para que tenga esto y compílelo nuevamente. |
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);
}
}
}
|