Exemple #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:CustomTypeProvider"/> class.
        /// </summary>
        /// <param name="provider">The provider.</param>
        public CustomTypeProvider(IServiceProvider provider, List <Project> additionalProjects, bool useProjectItemWrapper)
        {
            this.useProjectItemWrapper = useProjectItemWrapper;
            DynamicTypeService typeService = (DynamicTypeService)provider.GetService(typeof(DynamicTypeService));

            Debug.Assert(typeService != null, "No dynamic type service registered.");

            availableTypes = new Dictionary <string, Type>();

            if (additionalProjects != null && additionalProjects.Count > 0)
            {
                foreach (Project project in additionalProjects)
                {
                    IVsHierarchy          additionalHierarchy = DteHelper.GetVsHierarchy(provider, project);
                    ITypeDiscoveryService additionalDiscovery = typeService.GetTypeDiscoveryService(additionalHierarchy);
                    AddTypes(additionalDiscovery, project);
                }
            }
            else
            {
                IVsHierarchy hier = DteHelper.GetCurrentSelection(provider);
                Debug.Assert(hier != null, "No active hierarchy is selected.");

                ITypeDiscoveryService discovery = typeService.GetTypeDiscoveryService(hier);
                Project dteProject = VSHelper.ToDteProject(hier);

                AddTypes(discovery, dteProject);
            }

            if (availableTypes.Count > 0 && TypesChanged != null)
            {
                TypesChanged(this, new EventArgs());
            }
        }
Exemple #2
0
        /// <summary>
        /// Returns a collection of standard values for the data type this type converter is designed for when provided with a format context.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that provides a format context that can be used to extract additional information about the environment from which this converter is invoked. This parameter or properties of this parameter can be null.</param>
        /// <returns>
        /// A <see cref="T:System.ComponentModel.TypeConverter.StandardValuesCollection"></see> that holds a standard set of valid values, or null if the data type does not support a standard set of values.
        /// </returns>
        public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            DynamicTypeService typeService = (DynamicTypeService)context.GetService(typeof(DynamicTypeService));

            Debug.Assert(typeService != null, "No dynamic type service registered.");

            IVsHierarchy hier = DteHelper.GetCurrentSelection(context);

            Debug.Assert(hier != null, "No active hierarchy is selected.");

            ITypeDiscoveryService typeDiscovery = typeService.GetTypeDiscoveryService(hier);

            if (typeDiscovery != null)
            {
                List <string> types = new List <string>();
                foreach (Type type in typeDiscovery.GetTypes(typeof(object), false))
                {
                    if (ShouldInclude(type))
                    {
                        types.Add(type.FullName);
                    }
                }

                types.Sort(StringComparer.CurrentCulture);

                return(new StandardValuesCollection(types));
            }
            else
            {
                return(new StandardValuesCollection(new List <string>()));
            }
        }
            /// <summary>
            /// Initializes a new instance of the <see cref="T:CustomTypeProvider"/> class.
            /// </summary>
            /// <param name="provider">The provider.</param>
            public CustomTypeProvider(IServiceProvider provider)
            {
                DynamicTypeService typeService = (DynamicTypeService)provider.GetService(typeof(DynamicTypeService));

                System.Diagnostics.Debug.Assert(typeService != null, "No dynamic type service registered.");

                IVsHierarchy hier = DteHelper2.GetCurrentSelection(provider);

                System.Diagnostics.Debug.Assert(hier != null, "No active hierarchy is selected.");

                ITypeDiscoveryService discovery = typeService.GetTypeDiscoveryService(hier);
                Project dteProject = VsHelper.ToDteProject(hier);

                availableTypes = new Dictionary <string, Type>();
                LoadTypes(availableTypes, discovery.GetTypes(typeof(object), false), dteProject);

                // Try loading Project references
                LoadProjectReferenceTypesFromCurrentProject(availableTypes, provider, dteProject);

                // If we don't get any type loaded, try with the rest of the projects in the current sln
                if (availableTypes.Count == 0)
                {
                    LoadTypesFromSolution(availableTypes, provider, dteProject);
                }

                EnsureKnownTypes(availableTypes);

                if (availableTypes.Count > 0 && TypesChanged != null)
                {
                    TypesChanged(this, new EventArgs());
                }
            }
Exemple #4
0
        /// <summary>
        /// Returns the discovered types for autocomplete
        /// </summary>
        protected virtual IEnumerable <Type> DiscoverTypes()
        {
            DynamicTypeService    typeService          = (DynamicTypeService)DataGridView.Site.GetService(typeof(DynamicTypeService));
            IVsHierarchy          hier                 = DteHelper.GetCurrentSelection(DataGridView.Site);
            ITypeDiscoveryService typeDiscoveryService = typeService.GetTypeDiscoveryService(hier);

            return((IEnumerable <Type>)typeDiscoveryService.GetTypes(typeof(object), false));
        }
Exemple #5
0
        private void InitTypeDiscoveryService()
        {
            if (_discovery != null)
            {
                return;
            }
            DynamicTypeService typeService = _services.TypeService;

            if (typeService != null)
            {
                _discovery = typeService.GetTypeDiscoveryService(GetHierarchy());
            }
        }
        private void LoadTypes()
        {
            if (loadingTypes)
            {
                return;
            }

            try
            {
                tracer.Verbose(Resources.ProjectTypeProvider_TraceLoadTypes);
                loadingTypes = true;

                this.availableTypes.Clear();

                foreach (var project in solution.Find <IProject>())
                {
                    var hierarchy = project.As <IVsHierarchy>();
                    if (hierarchy != null)
                    {
                        tracer.Verbose(Resources.ProjectTypeProvider_TraceLoadProjectTypes, project.Name);
                        var    typeDiscoveryService = dynamicTypeService.GetTypeDiscoveryService(hierarchy);
                        string currentProjectAssemblyName;

                        try
                        {
                            currentProjectAssemblyName = project.As <EnvDTE.Project>().Properties.Item(@"AssemblyName").Value as string;
                        }
                        catch (Exception)
                        {
                            continue;
                        }

                        // We should be able to leverage Progression for this!
                        var types = typeDiscoveryService.GetTypes(typeof(object), true);

                        foreach (Type type in types)
                        {
                            if (string.Equals(type.Assembly.GetName().Name, currentProjectAssemblyName, StringComparison.InvariantCultureIgnoreCase))
                            {
                                availableTypes.Add(type);
                            }
                        }
                    }
                }
            }
            finally
            {
                loadingTypes = false;
            }
        }
Exemple #7
0
        /// <summary>
        /// Returns the type discovery service for the given project
        /// </summary>
        /// <param name="project">The project to use for the vsHierarchy</param>
        /// <returns>The type discovery service or null if it cannot be found</returns>
        /// <exception cref="WizardCancelledException"> is thrown if no hierarchy is available.</exception>
        private ITypeDiscoveryService GetTypeDiscoveryService(Project project)
        {
            DynamicTypeService dts = this.GetService(typeof(DynamicTypeService)) as DynamicTypeService;

            if (dts == null)
            {
                return(null);
            }

            // Get the hierarchy.  Throws if not available.
            IVsHierarchy vsHierarchy = this.GetVsHierarchy(project);

            ITypeDiscoveryService tds = dts.GetTypeDiscoveryService(vsHierarchy);

            return(tds);
        }
            private void LoadTypesFromProjects(List <EnvDTE.Project> projects,
                                               Dictionary <string, Type> availableTypes,
                                               IServiceProvider provider,
                                               Project currentProject)
            {
                DynamicTypeService typeService = (DynamicTypeService)provider.GetService(typeof(DynamicTypeService));

                foreach (Project project in projects)
                {
                    if (project.UniqueName != currentProject.UniqueName)
                    {
                        IVsHierarchy hier = DteHelper2.GetVsHierarchy(provider, project);
                        System.Diagnostics.Debug.Assert(hier != null, "No active hierarchy is selected.");
                        ITypeDiscoveryService discovery = typeService.GetTypeDiscoveryService(hier);
                        LoadTypes(availableTypes, discovery.GetTypes(typeof(object), false), project);
                    }
                }
            }
Exemple #9
0
        public static List <Type> GetProjectTypes(Type baseType, IServiceProvider serviceProvider, Projects projects)
        {
            IVsSolution        solution     = (IVsSolution)serviceProvider.GetService(typeof(IVsSolution));
            DynamicTypeService typeResolver = (DynamicTypeService)serviceProvider.GetService(typeof(DynamicTypeService));

            List <Type> result = new List <Type>();

            for (int i = 1; i <= projects.Count; i++)
            {
                Project      project   = projects.Item(i);
                IVsHierarchy hierarchy = null;
                solution.GetProjectOfUniqueName(project.UniqueName, out hierarchy);

                //_typeResolutionService = typeResolver.GetTypeResolutionService(hierarchy);
                var typeDiscoveryService             = typeResolver.GetTypeDiscoveryService(hierarchy);
                System.Collections.ICollection fined = typeDiscoveryService.GetTypes(baseType, true);
                foreach (Type t in fined)
                {
                    result.Add(t);
                }
            }
            return(result);
        }
Exemple #10
0
        /// <summary>
        /// Edits the specified object's value using the editor style indicated by the <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle"></see> method.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that can be used to gain additional context information.</param>
        /// <param name="provider">An <see cref="T:System.IServiceProvider"></see> that this editor can use to obtain services.</param>
        /// <param name="value">The object to edit.</param>
        /// <returns>
        /// The new value of the object. If the value of the object has not changed, this should return the same object it was passed.
        /// </returns>
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            DynamicTypeService typeService = (DynamicTypeService)provider.GetService(typeof(DynamicTypeService));

            IVsHierarchy hier = DteHelper.GetCurrentSelection(provider);

            ITypeDiscoveryService typeDiscovery = typeService.GetTypeDiscoveryService(hier);

            Project project = ToDteProject(hier);

            if (DteHelper.IsWebProject(project))
            {
                VSWebSite     vsProject  = (VSWebSite)project.Object;
                List <string> assemblies = new List <string>();
                foreach (AssemblyReference reference in vsProject.References)
                {
                    if (!string.IsNullOrEmpty(reference.FullPath))
                    {
                        assemblies.Add(reference.FullPath);
                    }
                }

                MethodInfo setAsssembliesMethod = typeDiscovery.GetType().GetMethod(SetAssembliesMethodName,
                                                                                    BindingFlags.NonPublic | BindingFlags.Instance);
                setAsssembliesMethod.Invoke(typeDiscovery, new object[] { assemblies.ToArray() });
            }

            if (typeDiscovery != null)
            {
                List <string>   assembliesAdded = new List <string>();
                List <Assembly> assemblies      = new List <Assembly>();
                List <Type>     types           = new List <Type>();

                foreach (Type type in typeDiscovery.GetTypes(typeof(object), false))
                {
                    if (ShouldInclude(type))
                    {
                        if (!assembliesAdded.Contains(type.Assembly.FullName))
                        {
                            assembliesAdded.Add(type.Assembly.FullName);
                            assemblies.Add(type.Assembly);
                        }
                        types.Add(type);
                    }
                }

                IWindowsFormsEditorService svc  = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                ClassBrowserEditorForm     form = new ClassBrowserEditorForm(assemblies, types);
                DialogResult result;

                if (svc != null)
                {
                    result = svc.ShowDialog(form);
                }
                else
                {
                    result = form.ShowDialog();
                }

                if (result == DialogResult.OK)
                {
                    return(form.TypeFullName);
                }
                else
                {
                    return(value);
                }
            }
            else
            {
                return(null);
            }
        }