I am trying to figure out how you can import and use DLLs at runtime inside a C # application. Using Assembly.LoadFile (), I was able to load my program to load the dll (this part definitely works, since I can get the class name with ToString ()), however I cannot use the 'Output' method from my console application. I compile .dll and move it to my console project. Is there an additional step between CreateInstance and the ability to use methods?
This is the class in my DLL:
namespace DLL { using System; public class Class1 { public void Output(string s) { Console.WriteLine(s); } } }
and here is the application that I want to download the DLL
namespace ConsoleApplication1 { using System; using System.Reflection; class Program { static void Main(string[] args) { var DLL = Assembly.LoadFile(@"C:\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\DLL.dll"); foreach(Type type in DLL.GetExportedTypes()) { var c = Activator.CreateInstance(type); c.Output(@"Hello"); } Console.ReadLine(); } } }
reflection c # dll
danbroooks Aug 21 '13 at 16:02 2013-08-21 16:02
source share