C# add a reference using only code (no IDE “Add Reference” functions)

前端 未结 4 1393
攒了一身酷
攒了一身酷 2021-01-13 07:57

I am writting a plugin for a program, and I want to put my code inside a DLL so I can share the plugin freely without exposing (giving away) my code.

Here is the bas

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-13 08:40

    Found exactly what I was looking for, here it is:

    using System.Reflection;
    using System.IO;
    
    try
    {
        Assembly a = null;
    
        a = Assembly.LoadFrom(Application.StartupPath startupPath + "MyAssembly.dll"); 
    
        Type classType = a.GetType("MyNamespace.MyClass");
        object obj = Activator.CreateInstance(classType);
        MethodInfo mi = classType.GetMethod("MyMethod");
    
        mi.Invoke(obj, null);
    }
    catch (Exception e)
    {                 
        AddLog(e.Message);            
    }
    

提交回复
热议问题