You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
5.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/////////////////////////////////////////////////////////////////
//
// (C) Copyright 2013, Kenneth, Inc.
// All rights reserved. Confidential. Except as pursuant
// to a written agreement with Kenneth, this software may
// not be used or distributed. This software may be covered
// by one or more patents.
//
// 本软件为Kenneth开发版权所有违者必究320325198102218110
//
/////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using Newtonsoft.Json;
namespace GummingCommon
{
/// <summary>
/// 后台服务调度引擎
/// </summary>
public sealed class ServiceEngine
{
/// <summary>
///
/// </summary>
/// <param name="errorCode"></param>
/// <returns></returns>
public static string DealErrorInfo(string errorCode)
{
OutEntity oe = new OutEntity() { ErrorCode = errorCode };
return JsonConvert.SerializeObject(oe);
}
private static object InvokeMethodBase(string assemblyName, string classType, string methodName, string currentUserId, string currentClientId, object currentSendParameter, Type paratype)
{
object outEntity = null;
//加载 Assembly
Assembly assembly = null;
if (assemblyName != "")
{
try
{
assembly = Assembly.Load(assemblyName);
}
catch (Exception ep)
{
throw new Exception(ErrorCode.LoadAssemblyError, ep);
}
}
else
{
return DealErrorInfo(ErrorCode.LoadAssemblyConfigError);
}
if (assembly == null)
{
return DealErrorInfo(ErrorCode.LoadAssemblyConfigError);
}
//加载类型
Type type = null;
if (classType != "")
{
try
{
type = assembly.GetType(classType);
}
catch (Exception ep)
{
throw new Exception(ErrorCode.GetAssemblyTypeError, ep);
}
}
else
{
return DealErrorInfo(ErrorCode.GetAssemblyTypeConfigError);
}
if (type == null)
{
return DealErrorInfo(ErrorCode.GetAssemblyTypeConfigError);
}
//加载类型
MethodInfo methodInfo = null;
if (methodName != "")
{
try
{
Type[] typeArray = new Type[3];
typeArray.SetValue(typeof(string), 0);
typeArray.SetValue(typeof(string), 1);
typeArray.SetValue(paratype, 2);
methodInfo = type.GetMethod(methodName, typeArray);
}
catch (Exception ep)
{
throw new Exception(ErrorCode.GetAssemblyMethodError, ep);
}
}
else
{
return DealErrorInfo(ErrorCode.GetAssemblyMethodConfigError);
}
if (methodInfo == null)
{
return DealErrorInfo(ErrorCode.GetAssemblyMethodConfigError);
}
//产生实例
Object objectInstance = null;
if (classType != "")
{
try
{
objectInstance = assembly.CreateInstance(classType);
}
catch (Exception ep)
{
throw new Exception(ErrorCode.CreateInstanceError, ep);
}
}
else
{
return DealErrorInfo(ErrorCode.CreateInstanceConfigError);
}
if (objectInstance == null)
{
return DealErrorInfo(ErrorCode.CreateInstanceConfigError);
}
//调用方法
try
{
object[] objectParameters = new object[] { currentUserId, currentClientId, currentSendParameter };
outEntity = methodInfo.Invoke(objectInstance, objectParameters);
}
catch (Exception ep)
{
throw new Exception(ErrorCode.InvokeMethodError, ep.InnerException);
}
return outEntity;
}
/// <summary>
///
/// </summary>
/// <param name="inEntity"></param>
/// <returns></returns>
public static string InvokeMethod(string assemblyName, string classType, string methodName, string currentUserId, string currentClientId, string currentSendParameter)
{
return (string)InvokeMethodBase(assemblyName, classType, methodName, currentUserId, currentClientId, currentSendParameter, typeof(string));
}
public static string WriteFile(string assemblyName, string classType, string methodName, string currentUserId, string currentClientId, TestphotofilesEntity file)
{
return (string)InvokeMethodBase(assemblyName, classType, methodName, currentUserId, currentClientId, file, typeof(TestphotofilesEntity));
}
public static byte[] ReadFile(string assemblyName, string classType, string methodName, string currentUserId, string currentClientId, string recid)
{
return (byte[])InvokeMethodBase(assemblyName, classType, methodName, currentUserId, currentClientId, recid, typeof(string));
}
}
}