public void IL(ScanAopInfo scanAopInfo) { foreach (var il in ILs) { il.IL(scanAopInfo); } }
public void SaveFiles(ScanAopInfo scanAopInfo) { //回写程序集 var assemblys = new Dictionary <string, AssemblyDefinition>(); foreach (var m in scanAopInfo.Methods) { if (!assemblys.ContainsKey(m.AssemblyPath)) { assemblys.Add(m.AssemblyPath, m.Assembly); } } foreach (var m in scanAopInfo.Types) { if (!assemblys.ContainsKey(m.AssemblyPath)) { assemblys.Add(m.AssemblyPath, m.Assembly); } } foreach (var a in assemblys) { bool pdb = IOHelper.HasPdbFile(a.Key); a.Value.Write(a.Key, new WriterParameters { WriteSymbols = pdb }); } }
/// <summary> /// 获取需要注入的Types和Methods /// </summary> public ScanAopInfo GetAopInfo(List <string> assemblieFiles, bool isreadmemory) { ScanAopInfo info = new ScanAopInfo(); foreach (var filepath in assemblieFiles) { //读取程序集 AssemblyDefinition assembly = null; //if (isreadmemory) //{ assembly = AssemblyDefinition.ReadAssembly(new System.IO.MemoryStream(System.IO.File.ReadAllBytes(filepath))); } //else bool pdb = IOHelper.HasPdbFile(filepath); { assembly = AssemblyDefinition.ReadAssembly(filepath, new ReaderParameters { ReadSymbols = pdb }); } //获取BaseAopAttribute类型的类型 //可以采用并行forearch加快速度,注意加lock // (Mono.Cecil.dll 似乎不支持并行,会报错,故实际并行度为1,并不采用并行,代码保留) foreach (var type in assembly.MainModule.Types) { if (CommonHelper.HasAttribute(type.CustomAttributes, typeof(BSF.Aop.Attributes.Base.BaseAopAttribute).FullName)) { info.Types.Add(new TypeAopInfo() { Assembly = assembly, Type = type, AssemblyPath = filepath }); } } //可以采用并行forearch加快速度,注意加lock // (Mono.Cecil.dll 似乎不支持并行,会报错,故实际并行度为1,并不采用并行,代码保留) foreach (var type in assembly.MainModule.Types) { foreach (var method in type.Methods) { //获取BaseAopAttribute类型的方法 var find = CommonHelper.HasAttribute(method.CustomAttributes, typeof(BSF.Aop.Attributes.Base.BaseAopAttribute).FullName); if (find == true) { info.Methods.Add(new MethodAopInfo { Assembly = assembly, Method = method, Type = type, AssemblyPath = filepath }); } } } } return(info); }