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

时间:2021-06-27 05:38:22

         第三方系统提供了多套接口,每套接口引用的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();
}
}
}