using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autofac;
using System.Reflection;
namespace EMIS.Utility
{
public class ReflectorHelper
{
///
/// Services方法
///
///
///
///
public static object RunMethod(string methodFullName, params object[] methodParameters)
{
IList 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));
}
}
}
///
/// DAL方法
///
///
///
///
public static object RunDALMethod(string methodFullName, params object[] methodParameters)
{
IList 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));
}
}
}
}
}