개발관련/C#

DLL 동적 로딩

Diademata 2017. 12. 18. 23:28
반응형

추상클래스로 변경함.


프로그램 실행될 때 경로에서 dll 읽어서 dll 안에 클래스 type이 ICallbackModule 인 경우 클래스 생성


var stream = new System.IO.StreamReader(new System.IO.FileStream(Environment.CurrentDirectory + @"\_conf\Config.json", System.IO.FileMode.Open, System.IO.FileAccess.Read));

var json = stream.ReadToEnd();

var jsonObj = Newtonsoft.Json.Linq.JObject.Parse(json);

var token = jsonObj["dll"];

foreach (var dll in token)

{

Assembly a = Assembly.LoadFile(Environment.CurrentDirectory + "\\" + dll.ToString());

Module[] modules = a.GetModules();

var types = a.GetExportedTypes();

foreach (var t in types)

{

try

{

if (t.GetTypeInfo().BaseType == typeof(ICallbackModule))

{

ICallbackModule instance = Activator.CreateInstance(t) as ICallbackModule;

instance.Init(this);

Console.WriteLine("Dll file load : " + dll.ToString());

}

}

catch (Exception ex)

{

throw ex;

}

}

}

반응형