Microsoft.VisualStudio.Project.ProjectElement.GetMetadataAndThrow C# (CSharp) Method

GetMetadataAndThrow() public method

Gets the attribute and throws the handed exception if the exception if the attribute is empty or null.
The method will throw an Exception and neglect the passed in exception if the attribute is deleted
public GetMetadataAndThrow ( string attributeName, Exception exception ) : string
attributeName string The name of the attribute to get.
exception System.Exception The exception to be thrown if not found or empty.
return string
        public string GetMetadataAndThrow(string attributeName, Exception exception)
        {
            if (attributeName == null)
                throw new ArgumentNullException("attributeName");
            if (string.IsNullOrEmpty(attributeName))
                throw new ArgumentException("attributeName cannot be null or empty");
            if (HasItemBeenDeleted)
                throw new InvalidOperationException("The item has been deleted.");

            Debug.Assert(!String.IsNullOrEmpty(attributeName), "Cannot retrieve an attribute for a null or empty attribute name");
            string attribute = GetMetadata(attributeName);

            if(String.IsNullOrEmpty(attributeName) && exception != null)
            {
                if(String.IsNullOrEmpty(exception.Message))
                {
                    Debug.Assert(!String.IsNullOrEmpty(this.ProjectManager.BaseUri.AbsoluteUrl), "Cannot retrieve an attribute for a project that does not have a name");
                    string message = String.Format(CultureInfo.CurrentCulture, SR.GetString(SR.AttributeLoad, CultureInfo.CurrentUICulture), attributeName, this.ProjectManager.BaseUri.AbsoluteUrl);
                    throw new InvalidOperationException(message, exception);
                }

                throw exception;
            }

            return attribute;
        }

Usage Example

Esempio n. 1
0
        /// <summary>
        /// Get information from the registry based for the project
        /// factory corresponding to the TypeGuid of the element
        /// </summary>
        private RegisteredProjectType GetRegisteredProject(ProjectElement element)
        {
            ProjectElement elementToUse = (element == null) ? this.nestedProjectElement : element;

            if (elementToUse == null)
            {
                throw new ArgumentNullException("element");
            }

            // Get the project type guid from project elementToUse
            string typeGuidString     = elementToUse.GetMetadataAndThrow(ProjectFileConstants.TypeGuid, new Exception());
            Guid   projectFactoryGuid = new Guid(typeGuidString);

            EnvDTE.DTE dte = this.ProjectMgr.Site.GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
            Debug.Assert(dte != null, "Could not get the automation object from the services exposed by this project");

            if (dte == null)
            {
                throw new InvalidOperationException();
            }

            RegisteredProjectType registeredProjectType = RegisteredProjectType.CreateRegisteredProjectType(projectFactoryGuid);

            Debug.Assert(registeredProjectType != null, "Could not read the registry setting associated to this project.");
            if (registeredProjectType == null)
            {
                throw new InvalidOperationException();
            }
            return(registeredProjectType);
        }
All Usage Examples Of Microsoft.VisualStudio.Project.ProjectElement::GetMetadataAndThrow