/// <summary>
 /// Adds the reference to the project
 /// </summary>
 public override void Execute()
 {
     if (DteHelper.IsWebProject(referringProject))
     {
         // This is a web project
         VSWebSite webProject = referringProject.Object as VSWebSite;
         if (webProject != null)
         {
             if (File.Exists(this.assemblyFilePath))
             {
                 webProject.References.AddFromFile(this.assemblyFilePath);
             }
             else
             {
                 webProject.References.AddFromGAC(this.assemblyFilePath);
             }
         }
     }
     else
     {
         // This is a standard project
         VSProject vsProject = referringProject.Object as VSProject;
         if (vsProject != null)
         {
             vsProject.References.Add(this.assemblyFilePath);
         }
     }
 }
예제 #2
0
            public string GetSerializationData(object target)
            {
                Project targetProject = target as Project;

                if (targetProject == null)
                {
                    throw new ArgumentException("target");
                }

                VSWebSite targetSite = targetProject.Object as VSWebSite;
                webType   kind       = (webType)targetProject.Properties.Item("WebSiteType").Value;

                string location = targetProject.UniqueName;

                if (kind == webType.webTypeFileSystem)
                {
                    string slnPath = Path.GetDirectoryName(targetProject.DTE.Solution.FileName);
                    if (targetProject.UniqueName.StartsWith(slnPath))
                    {
                        location = targetProject.UniqueName.Replace(slnPath, "");
                    }
                    // If not under the solution, we will persist the full path. It will
                    // work as long as the developer does not move the website around.
                    // A warning is issued at connection or construction time.
                }

                return(String.Join("|", new string[] { kind.ToString(), location }));
            }
예제 #3
0
 public void AddReference(string assembly)
 {
     ThreadHelper.ThrowIfNotOnUIThread();
     try
     {
         if (IsWebProject)
         {
             VSWebSite website = this.project.Object as VSWebSite;
             website.References.AddFromGAC(assembly);
         }
         else if (this.project.Object is VSProject2 prj2)
         {
             prj2.References.Add(assembly);
         }
         else
         {
             VSProject prj = this.project.Object as VSProject;
             prj.References.Add(assembly);
         }
     }
     catch (Exception ex)
     {
         Trace.WriteLine($"Failure adding reference to assembly {assembly}: {ex.Message}");
     }
 }
        /// <summary>
        /// Adds the project reference to the <see cref="ReferencedProject"/>
        /// </summary>
        public override void Execute()
        {
            if (this.ReferencedProject.UniqueName.Equals(this.ReferringProject.UniqueName, StringComparison.InvariantCultureIgnoreCase))
            {
                // Do nothing.
                return;
            }
            // If reference already exists, nothing happens.
            // If referringProject is a VSProject
            VSProject vsProject = referringProject.Object as VSProject;

            if (vsProject != null)
            {
                if (referencedProject.Kind.Equals(WebProjectKind, StringComparison.InvariantCultureIgnoreCase))
                {
                    VsWebSite.VSWebSite vsWebSite = (VsWebSite.VSWebSite)(referencedProject.Object);
                    foreach (VsWebSite.WebService webService in vsWebSite.WebServices)
                    {
                        vsProject.AddWebReference(webService.URL);
                    }
                }
                else
                {
                    // No error handling needed here. See documentation for AddProject.
                    vsProject.References.AddProject(referencedProject);
                }
            }
            else
            {
                // If the Project recived is not a VsProject
                // Try it with a webProject
                VSWebSite webProject = referringProject.Object as VSWebSite;
                if (webProject != null)
                {
                    if (referencedProject.Kind.Equals(WebProjectKind, StringComparison.InvariantCultureIgnoreCase))
                    {
                        VSWebSite vsWebSite = (VSWebSite)(referencedProject.Object);
                        foreach (VsWebSite.WebService webService in vsWebSite.WebServices)
                        {
                            webProject.WebReferences.Add(webService.URL, webService.ClassName);
                        }
                    }
                    else
                    {
                        // Check if the reference already exists in the WebProject
                        if (!IsAlreadyReferenced(webProject, referencedProject))
                        {
                            webProject.References.AddFromProject(referencedProject);
                        }
                    }
                }
            }
        }
예제 #5
0
        public string AddCodeFolderToWebProject(string name)
        {
            Debug.Assert(IsWebProject, "Cannot add code folders to a web project.");
            VSWebSite website      = (VSWebSite)project.Object;
            string    relativePath = string.Format(@"App_Code/{0}", name);

            website.CodeFolders.Add(relativePath);

            CodeFolder appCodeFolder = website.CodeFolders.Item(1);

            return(Path.Combine(appCodeFolder.ProjectItem.get_FileNames(0), name));
        }
예제 #6
0
        private bool IsAlreadyReferenced(VSWebSite webProject, Project referencedProject)
        {
            foreach (AssemblyReference reference in webProject.References)
            {
                if (reference.Name.Equals(ReferencedProject.Name, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(true);
                }
            }

            return(false);
        }
        private bool IsAlreadyReferenced(VSWebSite webProject, Project referencedProject)
        {
            foreach (AssemblyReference reference in webProject.References)
            {
                if (reference.Name.Equals(ReferencedProject.Name))
                {
                    return(true);
                }
            }

            return(false);
        }
예제 #8
0
 public void AddReference(string assembly)
 {
     if (IsWebProject)
     {
         VSWebSite website = this.project.Object as VSWebSite;
         website.References.AddFromGAC(assembly);
     }
     else
     {
         VSProject2 prj = this.project.Object as VSProject2;
         prj.References.Add(assembly);
     }
 }
예제 #9
0
            private IEnumerator <Project> IterateWebProjectReferences(VSWebSite vsWebSite)
            {
                if (vsWebSite.References == null)
                {
                    yield break;
                }

                foreach (AssemblyReference reference in vsWebSite.References)
                {
                    if (reference.ReferencedProject != null)
                    {
                        yield return(reference.ReferencedProject);
                    }
                }
            }
예제 #10
0
        /// <summary>
        /// Finds the type of the code element from the specified project.
        /// </summary>
        /// <param name="project">The project.</param>
        /// <param name="typeName">Name of the type.</param>
        /// <param name="elementKind">Kind of the element.</param>
        /// <returns>
        /// The <see cref="CodeElement"/> found or null if no matches.
        /// </returns>
        public static CodeElement FindCodeElementFromType(Project project, string typeName, vsCMElement elementKind)
        {
            if (project == null ||
                string.IsNullOrEmpty(typeName))
            {
                return(null);
            }

            // try to find it in the selected project
            CodeElement result = FindCodeElementByFullName(project, typeName, elementKind);

            if (result == null)
            {
                // navigate through the project references and look into each project
                if (!DteHelper.IsWebProject(project))
                {
                    VSProject vsProject = project.Object as VSProject;
                    foreach (Reference reference in vsProject.References)
                    {
                        result = FindCodeElementFromType(reference.SourceProject, typeName, elementKind);
                        if (result != null &&
                            result.InfoLocation == vsCMInfoLocation.vsCMInfoLocationProject)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    VSWebSite webProject = project.Object as VSWebSite;
                    if (webProject != null)
                    {
                        foreach (AssemblyReference reference in webProject.References)
                        {
                            Project sourceProject = GetSourceProject(reference);
                            result = FindCodeElementFromType(sourceProject, typeName, elementKind);
                            if (result != null &&
                                result.InfoLocation == vsCMInfoLocation.vsCMInfoLocationProject)
                            {
                                break;
                            }
                        }
                    }
                }
            }

            return(result);
        }
예제 #11
0
        private static void AddMissingReferencesForWebsite(VSWebSite webSiteProject, ICollection <string> referenceFileNames)
        {
            // first convert the enumerable into a hash for quick lookup
            var websiteReferenceHash       = new HashSet <string>();
            var websiteReferenceEnumerator = webSiteProject.References.GetEnumerator();
            var netRefPath = EdmUtils.GetRuntimeAssemblyPath(webSiteProject.Project, Services.ServiceProvider);

            while (websiteReferenceEnumerator.MoveNext())
            {
                var assemblyReference = websiteReferenceEnumerator.Current as AssemblyReference;
                if (assemblyReference != null)
                {
                    websiteReferenceHash.Add(assemblyReference.Name);
                }
            }

            foreach (var referenceFileName in referenceFileNames)
            {
                if (!websiteReferenceHash.Contains(referenceFileName))
                {
                    // first try the GAC
                    try
                    {
                        webSiteProject.References.AddFromGAC(referenceFileName);
                    }
                    catch (FileNotFoundException)
                    {
                        // attempt to add the file from the net framework directory.
                        // TODO: We should check OOB DataFx Installation folder first before looking at .net framework folder.
                        // Tracked by bug: 740496
                        try
                        {
                            webSiteProject.References.AddFromFile(Path.Combine(netRefPath, referenceFileName));
                        }
                        catch (FileNotFoundException)
                        {
                            // can't do anything else; leave it up to the build process to pick up the
                            // errors and encourage the user to add the references manually.
                        }
                    }
                }
            }
        }
        private void CacheWebsiteReferences(VSWebSite vsWebSite)
        {
            foreach (AssemblyReference reference in vsWebSite.References)
            {
                // FullPath is non-empty for everything except GAC'd assemblies, which our schema context does not care about.
                // We will use this to determine the assembly name without path or extension, which is equivalent to the 'Identity'.
                var indexOfLastBackslash = reference.FullPath.LastIndexOf('\\');
                var startOfAssemblyName = indexOfLastBackslash != -1 ? indexOfLastBackslash + 1 : 0;
                var assemblyName = Path.GetFileNameWithoutExtension(reference.FullPath.Substring(startOfAssemblyName));

                if (_assembliesInstalledUnderVisualStudio.ContainsKey(assemblyName.ToUpperInvariant()))
                {
                    // Ignore these DLLs - should be loaded using _assembliesInstalledUnderVisualStudio instead
                    continue;
                }

                if (!_websiteReferenceLookup.ContainsKey(assemblyName))
                {
                    _websiteReferenceLookup.Add(assemblyName, reference);
                }
            }
        }
예제 #13
0
 private bool Refresh()
 {
     if (IsWebProject)
     {
         VSWebSite website = this.project.Object as VSWebSite;
         if (website != null)
         {
             website.Refresh();
             return(true);
         }
     }
     else
     {
         VSProject2 project = this.project.Object as VSProject2;
         if (project != null)
         {
             project.Refresh();
             return(true);
         }
     }
     return(false);
 }
        private void CacheWebsiteReferences(VSWebSite vsWebSite)
        {
            foreach (AssemblyReference reference in vsWebSite.References)
            {
                // FullPath is non-empty for everything except GAC'd assemblies, which our schema context does not care about.
                // We will use this to determine the assembly name without path or extension, which is equivalent to the 'Identity'.
                var indexOfLastBackslash = reference.FullPath.LastIndexOf('\\');
                var startOfAssemblyName  = indexOfLastBackslash != -1 ? indexOfLastBackslash + 1 : 0;
                var assemblyName         = Path.GetFileNameWithoutExtension(reference.FullPath.Substring(startOfAssemblyName));

                if (_assembliesInstalledUnderVisualStudio.ContainsKey(assemblyName.ToUpperInvariant()))
                {
                    // Ignore these DLLs - should be loaded using _assembliesInstalledUnderVisualStudio instead
                    continue;
                }

                if (!_websiteReferenceLookup.ContainsKey(assemblyName))
                {
                    _websiteReferenceLookup.Add(assemblyName, reference);
                }
            }
        }
예제 #15
0
 private bool Refresh()
 {
     ThreadHelper.ThrowIfNotOnUIThread();
     if (IsWebProject)
     {
         VSWebSite website = this.project.Object as VSWebSite;
         if (website != null)
         {
             website.Refresh();
             return(true);
         }
     }
     else
     {
         VSProject2 project = this.project.Object as VSProject2;
         if (project != null)
         {
             project.Refresh();
             return(true);
         }
     }
     return(false);
 }
        /// <summary>
        /// Executes the action
        /// </summary>
        public override void Execute()
        {
            bool          gaced      = (_assembliesPath == null);
            List <string> assemblies = BuildAssembliesPathList(gaced);

            if (DteHelper.IsWebProject(_referringProject))
            {
                // This is a web project
                VSWebSite webProject = _referringProject.Object as VSWebSite;
                if (webProject != null)
                {
                    foreach (string assemblyFilePath in assemblies)
                    {
                        if (gaced)
                        {
                            webProject.References.AddFromGAC(assemblyFilePath);
                        }
                        else
                        {
                            webProject.References.AddFromFile(assemblyFilePath);
                        }
                    }
                }
            }
            else
            {
                // standard project
                VSProject vsProject = _referringProject.Object as VSProject;
                if (vsProject != null)
                {
                    foreach (string assemblyFilePath in assemblies)
                    {
                        vsProject.References.Add(assemblyFilePath);
                    }
                }
            }
        }
        private static void AddMissingReferencesForWebsite(VSWebSite webSiteProject, ICollection<string> referenceFileNames)
        {
            // first convert the enumerable into a hash for quick lookup
            var websiteReferenceHash = new HashSet<string>();
            var websiteReferenceEnumerator = webSiteProject.References.GetEnumerator();
            var netRefPath = EdmUtils.GetRuntimeAssemblyPath(webSiteProject.Project, Services.ServiceProvider);

            while (websiteReferenceEnumerator.MoveNext())
            {
                var assemblyReference = websiteReferenceEnumerator.Current as AssemblyReference;
                if (assemblyReference != null)
                {
                    websiteReferenceHash.Add(assemblyReference.Name);
                }
            }

            foreach (var referenceFileName in referenceFileNames)
            {
                if (!websiteReferenceHash.Contains(referenceFileName))
                {
                    // first try the GAC
                    try
                    {
                        webSiteProject.References.AddFromGAC(referenceFileName);
                    }
                    catch (FileNotFoundException)
                    {
                        // attempt to add the file from the net framework directory.
                        // TODO: We should check OOB DataFx Installation folder first before looking at .net framework folder.
                        // Tracked by bug: 740496
                        try
                        {
                            webSiteProject.References.AddFromFile(Path.Combine(netRefPath, referenceFileName));
                        }
                        catch (FileNotFoundException)
                        {
                            // can't do anything else; leave it up to the build process to pick up the
                            // errors and encourage the user to add the references manually.
                        }
                    }
                }
            }
        }
        private bool IsAlreadyReferenced(VSWebSite webProject, Project referencedProject)
        {
            foreach(AssemblyReference reference in webProject.References)
            {
                if(reference.Name.Equals(ReferencedProject.Name, StringComparison.InvariantCultureIgnoreCase))
                {
                    return true;
                }
            }

            return false;
        }
예제 #19
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);
            }
        }