コード例 #1
0
        public SelectClassFromAssembliesView(ClassEntities classEntities, String sourceCommandName, String xamlFileClassName)
        {
            if (classEntities == null)
            {
                throw new ArgumentNullException(nameof(classEntities));
            }
            if (sourceCommandName == null)
            {
                throw new ArgumentNullException(nameof(sourceCommandName));
            }

            var classEntitiesCollectionView = CollectionViewSource.GetDefaultView(classEntities) as CollectionView;

            if (classEntitiesCollectionView == null)
            {
                throw new NullReferenceException();
            }

            // ReSharper disable PossibleNullReferenceException
            classEntitiesCollectionView.GroupDescriptions.Clear();

            // ReSharper restore PossibleNullReferenceException
            classEntitiesCollectionView.SortDescriptions.Clear();
            classEntitiesCollectionView.GroupDescriptions.Add(new PropertyGroupDescription("AssemblyName"));
            classEntitiesCollectionView.GroupDescriptions.Add(new PropertyGroupDescription("NamespaceName"));
            classEntitiesCollectionView.SortDescriptions.Add(new SortDescription("AssemblyName", ListSortDirection.Ascending));
            classEntitiesCollectionView.SortDescriptions.Add(new SortDescription("NamespaceName", ListSortDirection.Ascending));
            classEntitiesCollectionView.SortDescriptions.Add(new SortDescription("ClassName", ListSortDirection.Ascending));

            InitializeComponent();

            this.tvObjects.ItemsSource = classEntitiesCollectionView.Groups;
            this.tbCommandCaption.Text = String.Concat("For ", sourceCommandName);

            this.tvObjects.SelectedItemChanged += tvObjects_SelectedItemChanged;
            this.lbViewModels.SelectionChanged += LbViewModels_SelectionChanged;

            this.lbViewModels.ItemsSource = classEntities.Where(x => x.ClassName.ToLower().EndsWith("viewmodel")).OrderBy(x => x.ClassName).ToList();
            foreach (ClassEntity item in this.lbViewModels.Items)
            {
                if (item.ClassName.StartsWith(xamlFileClassName))
                {
                    this.lbViewModels.SelectedItem = item;
                    break;
                }
            }
        }
コード例 #2
0
        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);
        }