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);
}
如果您觉得本文对你有用,不妨帮忙点个赞,或者在评论里给我一句赞美,小小成就都是今后继续为大家编写优质文章的动力!
欢迎您持续关注我的博客:)
版权所有,欢迎保留原文链接进行转载:)
浙公网安备 33010602011771号