private void AnalyzeAssembly(string assemblyFileName, string assemblyFilePath) { ProgressObject progressObject = new ProgressObject(assemblyFileName, assemblyFilePath); searchService.AsyncAnalyze(progressObject); }
public void AsyncAnalyze(ProgressObject progressObject) { Thread searchThread = new Thread(() => { IAssemblyService assemblyService = new AssemblyService(); IDeveloperService developerService = new DeveloperService(); IMethodService methodService = new MethodService(developerService); Exception runException = null; AssemblyModel assemblyModel = null; try { Globals.LoadOpCodes(); string filePath = progressObject.CurrAssemblyFilePath; Assembly assembly = Assembly.LoadFile(filePath); string assemblyFileNama = Path.GetFileName(filePath); string assemblyName = assembly.ManifestModule.ScopeName; string assemblyVersion = assembly.GetName().Version.ToString(); object[] guids = assembly.GetCustomAttributes(typeof(GuidAttribute), false); string guidString = string.Empty; if (guids.Length > 0) guidString = ((GuidAttribute)(guids[0])).Value; FileInfo fileInfo = new FileInfo(filePath); long filelength = fileInfo.Length; assemblyModel = new AssemblyModel(assemblyName, progressObject.CurrAssemblyFileName, assemblyVersion, guidString, filelength); Type[] foundTypes = assembly.GetTypes(); decimal totalLength = foundTypes.Length; decimal currProgress = 0; int currProgressPercentage = 0; foreach (Type t in foundTypes) { if (isCancel) break; if (ProgressChanged != null) { int percentage = (int)(Math.Round((++currProgress / totalLength), 2) * 100); progressObject.CurrTypeName = t.Name; if (currProgressPercentage != percentage) { currProgressPercentage = percentage; ReportProgress(percentage, progressObject); } } object[] typeAttrs = t.GetCustomAttributes(false); CreaterAttribute classCreaterAttribute = null; ReviewerAttribute[] classReviewerAttributes = null; if (typeAttrs.Where(c => c is CreaterAttribute).Count() > 0) classCreaterAttribute = typeAttrs.Where(c => c is CreaterAttribute).Cast<CreaterAttribute>().First(); if (typeAttrs.Where(r => r is ReviewerAttribute).Count() > 0) classReviewerAttributes = typeAttrs.Where(r => r is ReviewerAttribute).Cast<ReviewerAttribute>().ToArray(); //只处理该模块下的方法,摒弃系统方法 MethodInfo[] methodInfos = t.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance); Attribute createrAttr = null; object[] objectAttrs = null; ReviewerAttribute[] reviewerAttrs = null; Attribute bugAttr = null; MethodModel methodModel = null; MethodBodyReader methodBody = null; string methodName = string.Empty; foreach (MethodInfo method in methodInfos) { methodName = string.Format("{0}.{1}", t.Name, method.Name); createrAttr = Attribute.GetCustomAttribute(method, typeof(CreaterAttribute)); objectAttrs = method.GetCustomAttributes(typeof(ReviewerAttribute), false); bugAttr = Attribute.GetCustomAttribute(method, typeof(BugAttribute)); bool? isBug = null;//null表示该方法没有测试过 CreaterAttribute createPerson = null; int methodCodeNum = 0; string methodCodeString = string.Empty; ReviewerAttribute[] reviews = null; if (createrAttr is CreaterAttribute) createPerson = (CreaterAttribute)createrAttr; else if (classCreaterAttribute is CreaterAttribute) createPerson = (CreaterAttribute)classCreaterAttribute; else createPerson = new CreaterAttribute("未标注的开发者", "未标注时间"); if (objectAttrs.Where(r => r is ReviewerAttribute).Count() > 0) reviewerAttrs = objectAttrs.Where(r => r is ReviewerAttribute).Cast<ReviewerAttribute>().ToArray(); if (classReviewerAttributes != null && reviewerAttrs != null) reviews = classReviewerAttributes.Concat(reviewerAttrs).ToArray(); else if (classReviewerAttributes != null && reviewerAttrs == null) reviews = classReviewerAttributes; else if (classReviewerAttributes == null && reviewerAttrs != null) reviews = reviewerAttrs; if (bugAttr is BugAttribute) { BugAttribute Attr = (BugAttribute)bugAttr; isBug = Attr.IsBug; } methodBody = new MethodBodyReader(method); methodCodeNum = methodBody.GetCodeNum(); methodCodeString = methodBody.GetBodyCode(); methodModel = new MethodModel(methodName, assemblyModel, createPerson, methodCodeNum, reviews, isBug, methodCodeString); methodService.FindNewMethod(methodModel); } } //该程序集解析完毕,开始汇总 if (!isCancel) { List<DeveloperModel> developerModels = developerService.GetAllDeveloper(assemblyModel); assemblyService.SetMethodNum(assemblyModel, developerModels.Sum(d => d.MethodNum)); assemblyService.SetILCodeNum(assemblyModel, developerModels.Sum(d => d.ILCodeNum)); assemblyService.SetDevelopers(assemblyModel, developerModels); } } catch (Exception err) { runException = err; } finally { assemblyService = null; methodService = null; developerService = null; } AsyncAnalyzeCompleted(assemblyModel, runException); }); searchThread.IsBackground = true; searchThread.Start(); }