InvokePlugin.cs 2.5 KB
using DocumentFormat.OpenXml.Spreadsheet;
using log4net;
using log4net.Repository.Hierarchy;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class InvokePlugin
    {
        static Assembly assembly;
        static Type type;
        static object instantiation;
        static bool isLoadok = false;
        private  ILog _log=null;
        static Dictionary<string, MethodInfo> MethodInfos = new Dictionary<string, MethodInfo>();
        public bool LoadDLL(string plugname, ILog log)
        {
            if (!isLoadok)
            {
                _log = log;
                isLoadok = _LoadDLL(plugname);               
            }             
            return isLoadok;
        }
       private bool _LoadDLL(string plugname)
        {
            if (!File.Exists("Config\\" + plugname + ".dll"))
            {
                _log.Error($"LoadDLL 未找到{plugname}.DLL");
                return false;
            }               
            try
            {
                assembly = Assembly.LoadFrom("config\\" + plugname + ".dll");
            }
            catch (Exception e)
            {
                _log.Error("LoadDLL", e);
                return false;
            }
            type = assembly.GetType("Plugin.Plugin");
            if (type == null)
            {
                return false;
            }
            instantiation = Activator.CreateInstance(type);
            _log.Info("LoadDLL success");
            string[] methods = new string[] { "RequestServer" };
            foreach (var meths in methods)
            {
                var method = type.GetMethod(meths);
                if (method != null)
                    MethodInfos.Add(meths, method);
            }
            _log.Info("LoadDLL Method:" + string.Join(",", MethodInfos.Keys));
            return true;
        }

        /// <summary>
        /// 替换键值对 值的数据
        /// </summary>
        /// <param name="dic"></param>
        /// <param name="httpmaeth">请求方式:0为httpget;1为httppost;2为soappost</param>
        /// <returns></returns>
        public Dictionary<string, string> RequestServer(Dictionary<string, string> dic,int httpmaeth)
        {
            if (!MethodInfos.ContainsKey("RequestServer"))
                return null;
            return (Dictionary<string, string>)MethodInfos["RequestServer"].Invoke(instantiation, new object[] { dic, httpmaeth });
        }
    }
}