这篇文章发布于 725 天前,部分信息可能已经发生变化。
OrBit MES对接U9:开发MES->U9接口
反射主要代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace UFIDA.U9.Cust.SHBC.OrBitMESToU9.HotLoad
{
public static class HotLoadDLL
{
public static object run(string jsonStr,string action)
{
object retrunobj=null;
string DllName = "UFIDA.U9.SzCusDev.JAR.InterfaceSVSHBCHot.dll";
string PathAndFile = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"ApplicationServer\Libs\" + DllName;
if (!File.Exists(PathAndFile))
{
new Exception("糟糕!反射文件:" + PathAndFile + "文件没找到.");
}
Assembly asm = Assembly.Load(File.ReadAllBytes(PathAndFile));
foreach (Type type in asm.GetTypes().Where(t => t.IsClass && t.Namespace.StartsWith("UFIDA.U9.SzCusDev.JAR.InterfaceSV.Syncers")).ToList())
{
// Do stuff here
object o = Activator.CreateInstance(type, null);
PropertyInfo propertyInfo = type.GetProperty("Action");
if (propertyInfo == null) continue;
string actionName = (string)propertyInfo.GetValue(o, null);
PropertyInfo propertyInfo2 = type.GetProperty("ProjectName");
string projectName = (string)propertyInfo2.GetValue(o, null);
PropertyInfo propertyInfo3 = type.GetProperty("InModelName");
string inModelName = (string)propertyInfo3.GetValue(o, null);
PropertyInfo propertyInfo4 = type.GetProperty("OutModelName");
if (actionName == action)
{
MethodInfo method = type.GetMethod("Do", new Type[] { typeof(string) });
try
{
object result = method.Invoke(o, new object[] { jsonStr });
if (result != null)
{
retrunobj = JsonConvert.SerializeObject(result);
}
}
catch (Exception ex)
{
if (ex.InnerException != null)
retrunobj = JsonConvert.SerializeObject(new OutModel
{
isSuccess = false,
message = ex.InnerException.Message
});
}
break;
}
}
if (retrunobj == null)
{
throw new Exception(string.Format("Action解析失败,Action类型:{0},请确认传入的Action是否正确", action));
}
return retrunobj;
}
}
}
调用方法:
public override object Do(object obj)
{
SyncService syncService = (SyncService) obj;
try
{
LoggerManager.GetLogger(((object) this).GetType()).Error("OrBitMES->U9 JSON Begin:" + syncService.InStr + " OrBitMES->U9 JSON END;");
InModel inModel = JsonConvert.DeserializeObject<InModel>(syncService.InStr);
string action = inModel.action;
string key = inModel.action;
if (!string.IsNullOrEmpty(inModel.projectName))
key = key + "-" + inModel.projectName;
var t1 = UFIDA.U9.Cust.SHBC.OrBitMESToU9.HotLoad.HotLoadDLL.run(syncService.InStr, action);
if (t1 == null && !string.IsNullOrEmpty(inModel.projectName))
{
t1 = UFIDA.U9.Cust.SHBC.OrBitMESToU9.HotLoad.HotLoadDLL.run(syncService.InStr, key);
}
if (t1 == null)
{
throw new Exception(string.Format("Action解析失败,Action类型:{0},请确认传入的Action是否正确", (object)inModel.action));
}
// return (object) JsonConvert.SerializeObject(((SyncerFactory.GetSyncer(key) ?? SyncerFactory.GetSyncer(action)) ?? throw new Exception(string.Format("Action解析失败,Action类型:{0},请确认传入的Action是否正确", (object) inModel.action))).Do(syncService.InStr));
return (object)JsonConvert.SerializeObject(t1);
}
catch (Exception ex)
{
return (object) JsonConvert.SerializeObject((object) new OutModel()
{
isSuccess = false,
message = ex.Message
});
}
}
修改业务逻辑,更新BP DLL到U9,成功覆盖BP文件,调用成功!