using NetWorkHelper.IBase;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
namespace NetWorkHelper.Helper
{
public static class ReflectionHelper
{
#region 获取类型
///
/// GetType 通过完全限定的类型名来加载对应的类型。typeAndAssName如"NetWorkHelper.Filters.SourceFilter,NetWorkHelper"。
/// 如果为系统简单类型,则可以不带程序集名称。
///
public static Type GetType(string typeAndAssName)
{
string[] names = typeAndAssName.Split(',');
if (names.Length < 2)
{
return Type.GetType(typeAndAssName);
}
return ReflectionHelper.GetType(names[0].Trim(), names[1].Trim());
}
///
/// GetType 加载assemblyName程序集中的名为typeFullName的类型。assemblyName不用带扩展名,如果目标类型在当前程序集中,assemblyName传入null
///
public static Type GetType(string typeFullName, string assemblyName)
{
if (assemblyName == null)
{
return Type.GetType(typeFullName);
}
//搜索当前域中已加载的程序集
Assembly[] asses = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly ass in asses)
{
string[] names = ass.FullName.Split(',');
if (names[0].Trim() == assemblyName.Trim())
{
return ass.GetType(typeFullName);
}
}
//加载目标程序集
Assembly tarAssem = Assembly.Load(assemblyName);
if (tarAssem != null)
{
return tarAssem.GetType(typeFullName);
}
return null;
}
#endregion
#region GetTypeFullName
public static string GetTypeFullName(Type t)
{
return t.FullName + "," + t.Assembly.FullName.Split(',')[0];
}
#endregion
#region LoadDerivedInstance
///
/// LoadDerivedInstance 将程序集中所有继承自TBase的类型实例化
///
/// 基础类型(或接口类型)
/// 目标程序集
/// TBase实例列表
public static IList LoadDerivedInstance(Assembly asm)
{
IList list = new List();
Type supType = typeof(TBase);
foreach (Type t in asm.GetTypes())
{
if (supType.IsAssignableFrom(t) && (!t.IsAbstract) && (!t.IsInterface))
{
TBase instance = (TBase)Activator.CreateInstance(t);
list.Add(instance);
}
}
return list;
}
#endregion
#region LoadDerivedType
///
/// LoadDerivedType 加载directorySearched目录下所有程序集中的所有派生自baseType的类型
///
/// 基类(或接口)类型
/// 搜索的目录
/// 是否搜索子目录中的程序集
/// 高级配置,可以传入null采用默认配置
/// 所有从BaseType派生的类型列表
public static IList LoadDerivedType(Type baseType, string directorySearched, bool searchChildFolder, TypeLoadConfig config)
{
if (config == null)
{
config = new TypeLoadConfig();
}
IList derivedTypeList = new List();
if (searchChildFolder)
{
ReflectionHelper.LoadDerivedTypeInAllFolder(baseType, derivedTypeList, directorySearched, config);
}
else
{
ReflectionHelper.LoadDerivedTypeInOneFolder(baseType, derivedTypeList, directorySearched, config);
}
return derivedTypeList;
}
#region TypeLoadConfig
public class TypeLoadConfig
{
#region Ctor
public TypeLoadConfig() { }
public TypeLoadConfig(bool copyToMem, bool loadAbstract, string postfix)
{
this.copyToMemory = copyToMem;
this.loadAbstractType = loadAbstract;
this.targetFilePostfix = postfix;
}
#endregion
#region CopyToMemory
private bool copyToMemory = false;
///
/// CopyToMem 是否将程序集拷贝到内存后加载
///
public bool CopyToMemory
{
get { return copyToMemory; }
set { copyToMemory = value; }
}
#endregion
#region LoadAbstractType
private bool loadAbstractType = false;
///
/// LoadAbstractType 是否加载抽象类型
///
public bool LoadAbstractType
{
get { return loadAbstractType; }
set { loadAbstractType = value; }
}
#endregion
#region TargetFilePostfix
private string targetFilePostfix = ".dll";
///
/// TargetFilePostfix 搜索的目标程序集的后缀名
///
public string TargetFilePostfix
{
get { return targetFilePostfix; }
set { targetFilePostfix = value; }
}
#endregion
}
#endregion
#region LoadDerivedTypeInAllFolder
private static void LoadDerivedTypeInAllFolder(Type baseType, IList derivedTypeList, string folderPath, TypeLoadConfig config)
{
ReflectionHelper.LoadDerivedTypeInOneFolder(baseType, derivedTypeList, folderPath, config);
string[] folders = Directory.GetDirectories(folderPath);
if (folders != null)
{
foreach (string nextFolder in folders)
{
ReflectionHelper.LoadDerivedTypeInAllFolder(baseType, derivedTypeList, nextFolder, config);
}
}
}
#endregion
#region LoadDerivedTypeInOneFolder
private static void LoadDerivedTypeInOneFolder(Type baseType, IList derivedTypeList, string folderPath, TypeLoadConfig config)
{
string[] files = Directory.GetFiles(folderPath);
foreach (string file in files)
{
if (config.TargetFilePostfix != null)
{
if (!file.EndsWith(config.TargetFilePostfix))
{
continue;
}
}
Assembly asm = null;
#region Asm
try
{
if (config.CopyToMemory)
{
byte[] addinStream = FileHelper.ReadFileReturnBytes(file);
asm = Assembly.Load(addinStream);
}
else
{
asm = Assembly.LoadFrom(file);
}
}
catch (Exception ee)
{
ee = ee;
}
if (asm == null)
{
continue;
}
#endregion
Type[] types = asm.GetTypes();
foreach (Type t in types)
{
if (t.IsSubclassOf(baseType) || baseType.IsAssignableFrom(t))
{
bool canLoad = config.LoadAbstractType ? true : (!t.IsAbstract);
if (canLoad)
{
derivedTypeList.Add(t);
}
}
}
}
}
#endregion
#endregion
#region SetProperty
///
/// SetProperty 如果list中的object具有指定的propertyName属性,则设置该属性的值为proValue
///
public static void SetProperty(IList