ReflectionHelper

 public static T GetInstance<T>(Assembly assembly, string fullNamespace)
{
return (T)assembly.CreateInstance(fullNamespace);
} public static T GetInstance<T>(string assemblyName, string fullNamespace)
{
var path = GetFullAssemblyPath(assemblyName);
return (T)Assembly.LoadFile(path).CreateInstance(fullNamespace);
} public static object GetInstance(string assemblyName, string fullNamespace)
{
var path = GetFullAssemblyPath(assemblyName);
return Assembly.LoadFile(path).CreateInstance(fullNamespace);
} public static object GetInstance(string typeInfo)
{
string[] types = typeInfo.Split(',');
object tempObject = null;
if (types.Length == 1)
{
tempObject = GetInstance<Object>(typeof(ReflectionHelper).GetType().Assembly, types[0].Trim()); }
else if (types.Length == 2)
{
tempObject = GetInstance<Object>(types[1].Trim(), types[0].Trim());
} return tempObject;
} public static Type GetType(Assembly assembly, string fullNamespace)
{
return assembly.CreateInstance(fullNamespace).GetType();
} public static Type GetType(string assemblyName, string fullNamespace)
{
Assembly assembly = Assembly.LoadFile(GetFullAssemblyPath(assemblyName));
return GetType(assembly, fullNamespace);
} public static Type GetType(string typeInfo)
{
string[] types = typeInfo.Split(',');
Type tempType = null;
if (types.Length == 1)
{
tempType = GetType(typeof(ReflectionHelper).Assembly, types[0]);
}
else if (types.Length == 2)
{
tempType = GetType(types[1].Trim(), types[0].Trim());
} return tempType;
} public static T GetInstanceFromXml<T>(string typeInfo, string filePath)
{
Type configType = GetType(typeInfo); if (configType == null)
{
throw new Exception(string.Format("occur error when initialize {0}!", filePath));
} return XmlHelper.ConvertToObject<T>(configType, filePath);
} public static string GetFullAssemblyPath(string assemblyName)
{
var path = AppDomain.CurrentDomain.BaseDirectory;
var debugPath = @"bin\Debug\";
if (!path.Substring(path.Length - debugPath.Length - 1, debugPath.Length)
.ToLower().Equals(debugPath.ToLower()))
{
path = string.Concat(path, @"bin\");
} return string.Concat(path, assemblyName);
}

  

上一篇:算法导论(第三版)Problems2(归并插入排序、数列逆序计算)


下一篇:webpack 项目实践