12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Autofac;
- using System.Reflection;
- namespace EMIS.Utility
- {
- public class ReflectorHelper
- {
- /// <summary>
- /// Services方法
- /// </summary>
- /// <param name="methodFullName"></param>
- /// <param name="methodParameters"></param>
- /// <returns></returns>
- public static object RunMethod(string methodFullName, params object[] methodParameters)
- {
- IList<string> names = methodFullName.Split('.').ToList();
- if (names.Count <= 2)
- {
- throw (new Exception("方法名输入错误。"));
- }
- string methodName = names.Last();
- names.RemoveAt(names.Count - 1);
- string className = string.Join(".", names);
- string classShortName = names.Last();
- var classType = Assembly.Load("EMIS.CommonLogic").GetType(className);
- if (classType == null)
- {
- classType = Assembly.Load("EMIS.ICommonLogic").GetType(className);
- }
- Type myInterface;
- if (classType.IsInterface)
- {
- myInterface = classType;
- }
- else
- {
- myInterface = classType.GetInterfaces().Where(x => x.Name == "I" + classShortName).FirstOrDefault();
- }
- using (var container = AutofacHelper.Container.BeginLifetimeScope())
- {
- var myClass = container.Resolve(myInterface);
- var myMethod = myClass.GetType().GetMethod(methodName);
- try
- {
- return myMethod.Invoke(myClass, methodParameters);
- }
- catch (Exception ex)
- {
- throw (new Exception(ex.InnerException.Message));
- }
- }
- }
- /// <summary>
- /// DAL方法
- /// </summary>
- /// <param name="methodFullName"></param>
- /// <param name="methodParameters"></param>
- /// <returns></returns>
- public static object RunDALMethod(string methodFullName, params object[] methodParameters)
- {
- IList<string> names = methodFullName.Split('.').ToList();
- if (names.Count <= 2)
- {
- throw (new Exception("方法名输入错误。"));
- }
- string methodName = names.Last();
- names.RemoveAt(names.Count - 1);
- string className = string.Join(".", names);
- string classShortName = names.Last();
- var classType = Assembly.Load("EMIS.DataLogic").GetType(className);
- using (var container = AutofacHelper.Container.BeginLifetimeScope())
- {
- var myClass = container.Resolve(classType);
- var myMethod = myClass.GetType().GetMethod(methodName);
- try
- {
- return myMethod.Invoke(myClass, methodParameters);
- }
- catch (Exception ex)
- {
- throw (new Exception(ex.InnerException.Message));
- }
- }
- }
- }
- }
|