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

SetMetadata() public method

Set an attribute on the project element
public SetMetadata ( string attributeName, string attributeValue ) : void
attributeName string Name of the attribute to set
attributeValue string Value to give to the attribute. Use null to delete the metadata definition.
return void
        public void SetMetadata(string attributeName, string attributeValue)
        {
            if (attributeName == null)
                throw new ArgumentNullException("attributeName");
            if (string.IsNullOrEmpty(attributeName))
                throw new ArgumentException("attributeName cannot be null or empty");
            if (string.Equals(attributeName, ProjectFileConstants.Include, StringComparison.OrdinalIgnoreCase))
                throw new ArgumentException("Use Rename to set the Include attribute.");
            if (HasItemBeenDeleted)
                throw new InvalidOperationException("The item has been deleted.");

            if(this.IsVirtual)
            {
                /* For virtual node, use our virtual property collection
                 * Remove followed by Add ensures that the key in the dictionary has the same case as attributeName
                 */
                virtualProperties.Remove(attributeName);
                virtualProperties.Add(attributeName, attributeValue);
                return;
            }

            // Check out the project file.
            if(!this.ProjectManager.QueryEditProjectFile(false))
            {
                throw Marshal.GetExceptionForHR(VSConstants.OLE_E_PROMPTSAVECANCELLED);
            }

            // Build Action is the type, not a property, so intercept
            if(String.Equals(attributeName, ProjectFileConstants.BuildAction, StringComparison.OrdinalIgnoreCase))
            {
                Item.ItemType = attributeValue;
                return;
            }

            if(attributeValue == null)
                Item.RemoveMetadata(attributeName);
            else
                Item.SetMetadataValue(attributeName, attributeValue);

            ProjectManager.SetProjectFileDirty(true);
        }

Usage Example

Esempio n. 1
0
        private ProjectElement GetProjectElementBasedOnInputFromComponentSelectorData()
        {
            ProjectElement element = new ProjectElement(this.ProjectMgr, this.typeName, ProjectFileConstants.COMReference);

            // Set the basic information regarding this COM component
            element.SetMetadata(ProjectFileConstants.Guid, this.typeGuid.ToString("B"));
            element.SetMetadata(ProjectFileConstants.VersionMajor, this.majorVersionNumber);
            element.SetMetadata(ProjectFileConstants.VersionMinor, this.minorVersionNumber);
            element.SetMetadata(ProjectFileConstants.Lcid, this.lcid);
            element.SetMetadata(ProjectFileConstants.Isolated, false.ToString());

            // See if a PIA exist for this component
            TypeLibConverter typelib = new TypeLibConverter();
            string           assemblyName;
            string           assemblyCodeBase;

            if (typelib.GetPrimaryInteropAssembly(this.typeGuid, Int32.Parse(this.majorVersionNumber, CultureInfo.InvariantCulture), Int32.Parse(this.minorVersionNumber, CultureInfo.InvariantCulture), Int32.Parse(this.lcid, CultureInfo.InvariantCulture), out assemblyName, out assemblyCodeBase))
            {
                element.SetMetadata(ProjectFileConstants.WrapperTool, WrapperToolAttributeValue.Primary.ToString().ToLowerInvariant());
            }
            else
            {
                // MSBuild will have to generate an interop assembly
                element.SetMetadata(ProjectFileConstants.WrapperTool, WrapperToolAttributeValue.TlbImp.ToString().ToLowerInvariant());
                element.SetMetadata(ProjectFileConstants.EmbedInteropTypes, true.ToString());
                element.SetMetadata(ProjectFileConstants.Private, true.ToString());
            }
            return(element);
        }
All Usage Examples Of Microsoft.VisualStudio.Project.ProjectElement::SetMetadata