Correct Way to Load Assembly, Find Class and Call Run() Method

前端 未结 5 906
刺人心
刺人心 2020-11-22 12:17

Sample console program.

class Program
{
    static void Main(string[] args)
    {
        // ... code to build dll ... not written yet ...
        Assembly a         


        
5条回答
  •  既然无缘
    2020-11-22 12:30

    You will need to use reflection to get the type "TestRunner". Use the Assembly.GetType method.

    class Program
    {
        static void Main(string[] args)
        {
            Assembly assembly = Assembly.LoadFile(@"C:\dyn.dll");
            Type type = assembly.GetType("TestRunner");
            var obj = (TestRunner)Activator.CreateInstance(type);
            obj.Run();
        }
    }
    

提交回复
热议问题