c# 动态调用Newtonsoft.Json.dll的类

         第三方系统提供了多套接口,每套接口引用的Newtonsoft.Json.dll还不一样,我本身系统也引用Newtonsoft.Json.dll,引起版本混乱,改为动态反射调用当前目录下的dll


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace LibFLCSharpSyntax.Tool
{
    class CDLL
    {
        public MethodInfo DeserializeObject = null;
        public MethodInfo SerializeObject = null;
        public Object obj = null;
    }
    public class ToolJson
    {
        private static object objSerializerSettings = null;
        private static CDLL JsonConvert = null;

        private static object obLock = string.Empty;

        private static void GetObject()
        {
            if (JsonConvert != null)
            {
                return;
            }

            lock (obLock)
            {
                try
                {
                    if (JsonConvert != null)
                    {
                        return;
                    }

                    string deserializerClassName = "Newtonsoft.Json.JsonSerializerSettings";
                    string convertClassName = "Newtonsoft.Json.JsonConvert", convertMethodName = "SerializeObject", deserializerMethodName = "DeserializeObject";

                    //绝对路径
                    var assemblyPath = Path.Combine(FilePath.GetCurrentPath(), "Newtonsoft.Json.dll");
                    if (!File.Exists(assemblyPath))
                    {
                        throw new Exception(string.Format("文件不存在!路径:{0}", assemblyPath));
                    }
                    //实例化此实例
                    Assembly dllAssembly = Assembly.LoadFrom(assemblyPath);
                    if (dllAssembly == null)
                    {
                        throw new StopException("加载此程序集失败!");
                    }

                    // 反序列化
                    var deserializertype = dllAssembly.GetType(deserializerClassName);
                    if (deserializertype == null)
                    {
                        throw new StopException(string.Format("获取此类的Type失败!typeName:{0}", deserializerClassName));
                    }
                    objSerializerSettings = Activator.CreateInstance(deserializertype);
                    if (objSerializerSettings == null)
                    {
                        throw new StopException(string.Format("实例化此类失败!typeName:{0}", deserializerClassName));
                    }


                    var convert = new CDLL();

                    //获取Type 
                    var converttype = dllAssembly.GetType(convertClassName);
                    if (converttype == null)
                    {
                        throw new StopException(string.Format("获取此类的Type失败!typeName:{0}", convertClassName));
                    }

                    实例化此类型
                    //convert.obj = Activator.CreateInstance(converttype);
                    //if (convert.obj == null)
                    //{
                    //    throw new StopException(string.Format("实例化此类失败!typeName:{0}", convertClassName));
                    //}

                    try
                    {
                        //获取属性
                        convert.SerializeObject = converttype.GetMethod(convertMethodName, new Type[] { typeof(string) });

                        //获取属性
                        var deserializerType= new Type[] { typeof(string), deserializertype };
                        var DeserializerFindMethods = converttype.GetMethods().Where(p => p.Name.Equals(deserializerMethodName) && p.IsGenericMethod).ToList();
                        foreach (var md in DeserializerFindMethods)
                        {
                            var pars = md.GetParameters();
                            if (pars.Length == deserializerType.Length)
                            {
                                int iCompare = 0;
                                while (iCompare < pars.Length)
                                {
                                    if (pars[iCompare].ParameterType.Name != deserializerType[iCompare].Name)
                                    {
                                        break;
                                    }
                                    iCompare++;
                                }
                                if (iCompare == pars.Length)
                                {
                                    convert.DeserializeObject = md;
                                    break;
                                }
                            }
                        }
                        if ( convert.DeserializeObject ==null )
                        {
                            throw new Exception("未找到反序列的方法声明!");
                        } 
                    }
                    catch (StopException ex)
                    {
                        throw ex;
                    }
                    catch (Exception ex)
                    {
                        if (ex.InnerException != null)
                            ex = ex.InnerException;
                        throw new Exception(ex.Message.ToString());
                    }

                    JsonConvert = convert;
                }
                catch (Exception ex)
                {
                    throw new Exception("动态加载dll失败:\r\n" + ex.Message);
                }
            }
        }

        public static T DeserializeObject<T>(string value)
        {
            GetObject();

            var DeserializeObject = JsonConvert.DeserializeObject.MakeGenericMethod(new Type[] { typeof(T) });
            return (T)DeserializeObject.Invoke(JsonConvert.obj, new object[] { value, objSerializerSettings });
        }

        public static string SerializeObject(object value)
        {
            GetObject();
            object result = JsonConvert.SerializeObject.Invoke(JsonConvert.obj, new object[] { value });
            return result == null ? "" : result.ToString();
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值