标签:api com name interface 动态装载 lib lin open summary
先写一个接口,来约定一下,如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LibraryApi { /// <summary> /// 插件api /// </summary> public interface openapi { /// <summary> /// 名称 /// </summary> string Name { get; } /// <summary> /// 作者 /// </summary> string Auth { get; } /// <summary> /// 网址 /// </summary> string Url { get; } /// <summary> /// 解密 /// </summary> /// <param name="md5">MD5</param> /// <returns></returns> string Decryption(string md5); } }
然后继承接口,并实现,代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Script.Serialization; namespace cmd5.com { public class Decrytion : LibraryApi.openapi { public string Name { get { return "wmd5解密插件v1.0"; } } public string Auth { get { return "小渣渣"; } } public string Url { get { return "http://www.wmd5.com/"; } } public string Decryption(string md5) { return "123"; } } }
最后动态装载dll插件,并且执行:
/// <summary> /// 装载dll插件 /// </summary> /// <param name="file"></param> /// <returns></returns> private bool LoadPlugin(string file) { Assembly dll = Assembly.LoadFile(file); foreach (var _every in dll.GetTypes()) { if (_every.GetInterface(typeof(LibraryApi.openapi).Name) != null) { LibraryApi.openapi api = System.Activator.CreateInstance(_every) as LibraryApi.openapi; return true; } } return false; }
标签:api com name interface 动态装载 lib lin open summary
原文地址:http://www.cnblogs.com/testsec/p/6095501.html