v9.6.2017.0808   重构正向工程,基于映射表查找数据库字段类型到实体类型的映射
大石头 编写于 2017-08-08 21:38:06
X
using System;
using System.IO;
using NewLife.Log;
using NewLife.Reflection;

namespace NewLife.Web
{
    /// <summary>插件助手</summary>
    public static class PluginHelper
    {
        /// <summary>加载插件</summary>
        /// <param name="typeName"></param>
        /// <param name="disname"></param>
        /// <param name="dll"></param>
        /// <param name="linkName"></param>
        /// <param name="urls">提供下载地址的多个目标页面</param>
        /// <returns></returns>
        public static Type LoadPlugin(String typeName, String disname, String dll, String linkName, String urls = null)
        {
            var type = typeName.GetTypeEx(true);
            if (type != null) return type;

            if (dll.IsNullOrEmpty()) return null;

            // 先检查当前目录,再检查插件目录
            var file = dll.GetFullPath();
            if (!File.Exists(file) && Runtime.IsWeb) file = "Bin".GetFullPath().CombinePath(dll);
            if (!File.Exists(file)) file = Setting.Current.GetPluginPath().CombinePath(dll);

            if (urls.IsNullOrEmpty()) urls = Setting.Current.PluginServer;

            // 如果本地没有数据库,则从网络下载
            if (!File.Exists(file))
            {
                XTrace.WriteLine("{0}不存在或平台版本不正确,准备联网获取 {1}", disname ?? dll, urls);

                var client = new WebClientX(true, true);
                client.Log = XTrace.Log;
                var dir = Path.GetDirectoryName(file);
                var file2 = client.DownloadLinkAndExtract(urls, linkName, dir);
                client.TryDispose();
            }
            if (!File.Exists(file))
            {
                XTrace.WriteLine("未找到 {0} {1}", disname, dll);
                return null;
            }

            type = typeName.GetTypeEx(true);
            return type;
        }
    }
}