IEnumerable <String> GetProjectReferences(Project sourceProject) { var list = new List <String>(); var vsProject = (VSProject)sourceProject.Object; foreach (Reference reference in vsProject.References) { if (AssemblyAssistant.SkipLoadingAssembly(reference.Name)) { continue; } if (String.IsNullOrEmpty(reference.Path)) { DialogAssistant.ShowInformationMessage($"The {reference.Name} reference is broken or unresolved. It will be ignored for now, but you need to correct it or removed the unused reference.", "Broken Reference Found"); continue; } list.Add(reference.Path); } return(list); }
public TypeReflectorResult SelectClassFromAllReferencedAssemblies(Project sourceProject, String xamlFileClassName, String sourceCommandName, ProjectType projectFrameworkType, String projectFrameworkVersion) { if (sourceProject == null) { throw new ArgumentNullException(nameof(sourceProject)); } if (string.IsNullOrWhiteSpace(sourceCommandName)) { throw new ArgumentException("Value cannot be null or white space.", nameof(sourceCommandName)); } if (!Enum.IsDefined(typeof(ProjectType), projectFrameworkType)) { throw new InvalidEnumArgumentException(nameof(projectFrameworkType), (int)projectFrameworkType, typeof(ProjectType)); } if (projectFrameworkType == ProjectType.Silverlight) { SetSilverlightInstallPath(); } var assemblyPath = AssemblyAssistant.GetAssemblyPath(sourceProject); if (String.IsNullOrWhiteSpace(assemblyPath)) { DialogAssistant.ShowInformationMessage("The project associated with the selected file is either not vb, cs or is blacklisted.", "Invalid Project"); return(null); } if (!File.Exists(assemblyPath)) { DialogAssistant.ShowInformationMessage("Project assembly is missing, please 'build' your solution.", "Unbuilt Project"); return(null); } var resolver = new DefaultAssemblyResolver(); resolver.AddSearchDirectory(Path.GetDirectoryName(assemblyPath)); if (projectFrameworkType == ProjectType.Silverlight) { resolver.AddSearchDirectory(_silverlightAssembliesPath); } var reader = new ReaderParameters { AssemblyResolver = resolver }; var classEntities = new ClassEntities(); var sourceProjectPath = Path.GetDirectoryName(assemblyPath); var sourceAssemblyDefinition = AssemblyDefinition.ReadAssembly(assemblyPath, reader); var assembliesToLoad = new Hashtable(); //load up all referenced assemblies for above assemblyPath foreach (AssemblyNameReference assemblyReference in sourceAssemblyDefinition.MainModule.AssemblyReferences) { if (!AssemblyAssistant.SkipLoadingAssembly(assemblyReference.Name)) { var assemblyFullPath = GetAssemblyFullPath(sourceProjectPath, assemblyReference.Name); if (!String.IsNullOrWhiteSpace(assemblyFullPath) && !assembliesToLoad.ContainsKey(assemblyFullPath.ToLower(CultureInfo.InvariantCulture))) { assembliesToLoad.Add(assemblyFullPath.ToLower(CultureInfo.InvariantCulture), String.Empty); } } } //load up all assemblies referenced in the project but that are not loaded yet. foreach (var projectReference in GetProjectReferences(sourceProject)) { if (!assembliesToLoad.ContainsKey(projectReference)) { assembliesToLoad.Add(projectReference.ToLower(), String.Empty); } } ReflectClasses(sourceAssemblyDefinition, projectFrameworkType, projectFrameworkVersion, classEntities, ActiveProject.Yes); foreach (var asyPath in assembliesToLoad.Keys) { var asyResolver = new DefaultAssemblyResolver(); asyResolver.AddSearchDirectory(Path.GetDirectoryName(assemblyPath)); if (projectFrameworkType == ProjectType.Silverlight) { resolver.AddSearchDirectory(_silverlightAssembliesPath); } var asyReader = new ReaderParameters { AssemblyResolver = asyResolver }; ReflectClasses(AssemblyDefinition.ReadAssembly(asyPath.ToString(), asyReader), projectFrameworkType, projectFrameworkVersion, classEntities, ActiveProject.No); } var listOfConverters = new List <String>(); listOfConverters.AddRange(classEntities.Where(x => x.ClassName.ToLower().EndsWith("converter")).Select(n => n.ClassName).ToList()); var view = new SelectClassFromAssembliesView(classEntities, sourceCommandName, xamlFileClassName); var result = view.ShowDialog(); if (result.HasValue && result.Value && view.SelectedClassEntity != null) { LoadPropertyInformation(view.SelectedClassEntity.TypeDefinition, view.SelectedClassEntity, assembliesToLoad, projectFrameworkType); return(new TypeReflectorResult(view.SelectedClassEntity, listOfConverters)); } return(null); }