BuildItem BuildItem BuildItem BuildItem Class

Definizione

Rappresenta un singolo elemento in un progetto di MSBuild.Represents a single item in an MSBuild project.

public ref class BuildItem
public class BuildItem
type BuildItem = class
Public Class BuildItem
Ereditarietà
BuildItemBuildItemBuildItemBuildItem

Esempi

Nell'esempio seguente viene creato un oggetto Project e viene utilizzato il metodo LoadXml per aggiungere contenuto al progetto.The following example creates a Project object and uses the LoadXml method to add content to the project. Per aggiungere, rimuovere e modificare elementi nel progetto vengono utilizzate le classi BuildItem, BuildItemGroup e BuildItemGroupCollection.The BuildItem, BuildItemGroup, and BuildItemGroupCollection classes are used to add, remove, and change items in the project.

using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Build.BuildEngine;

namespace AddNewItem
{
    class Program
    {
        /// <summary>
        /// This code demonstrates the use of the following methods:
        ///     Engine constructor
        ///     Project constructor
        ///     Project.LoadFromXml
        ///     Project.Xml
        ///     BuildItemGroupCollection.GetEnumerator
        ///     BuildItemGroup.GetEnumerator
        ///     BuildItem.Name (get)
        ///     BuildItem.Include (set)
        ///     BuildItem.GetMetadata
        ///     BuildItem.SetMetadata
        ///     BuildItemGroup.RemoveItem
        ///     BuildItemGroup.AddNewItem
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            // Create a new Engine object.
            Engine engine = new Engine(Environment.CurrentDirectory);

            // Create a new Project object.
            Project project = new Project(engine);

            // Load the project with the following XML, which contains
            // two ItemGroups.
            project.LoadXml(@"
                <Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>

                    <ItemGroup>
                        <Compile Include='Program.cs'/>
                        <Compile Include='Class1.cs'/>
                        <RemoveThisItemPlease Include='readme.txt'/>
                    </ItemGroup>

                    <ItemGroup>
                        <EmbeddedResource Include='Strings.resx'>
                            <LogicalName>Strings.resources</LogicalName>
                            <Culture>fr-fr</Culture>
                        </EmbeddedResource>
                    </ItemGroup>

                </Project>
                ");

            // Iterate through each ItemGroup in the Project.  There are two.
            foreach (BuildItemGroup ig in project.ItemGroups)
            {
                BuildItem itemToRemove = null;

                // Iterate through each Item in the ItemGroup.
                foreach (BuildItem item in ig)
                {
                    // If the item's name is "RemoveThisItemPlease", then
                    // store a reference to this item in a local variable,
                    // so we can remove it later.
                    if (item.Name == "RemoveThisItemPlease")
                    {
                        itemToRemove = item;
                    }

                    // If the item's name is "EmbeddedResource" and it has a metadata Culture
                    // set to "fr-fr", then ...
                    if ((item.Name == "EmbeddedResource") && (item.GetMetadata("Culture") == "fr-fr"))
                    {
                        // Change the item's Include path to "FrenchStrings.fr.resx", 
                        // and add a new metadata Visiable="false".
                        item.Include = @"FrenchStrings.fr.resx";
                        item.SetMetadata("Visible", "false");
                    }
                }

                // Remove the item named "RemoveThisItemPlease" from the
                // ItemGroup
                if (itemToRemove != null)
                {
                    ig.RemoveItem(itemToRemove);
                }

                // For each ItemGroup that we found, add to the end of it
                // a new item Content with Include="SplashScreen.bmp".
                ig.AddNewItem("Content", "SplashScreen.bmp");
            }

            // The project now looks like this:
            //
            //     <?xml version="1.0" encoding="utf-16"?>
            //     <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
            //         <Compile Include="Program.cs" />
            //         <Compile Include="Class1.cs" />
            //         <Content Include="SplashScreen.bmp" />
            //         <EmbeddedResource Include="FrenchStrings.fr.resx">
            //           <LogicalName>Strings.resources</LogicalName>
            //           <Culture>fr-fr</Culture>
            //           <Visible>false</Visible>
            //         <Content Include="SplashScreen.bmp" />
            //
            Console.WriteLine(project.Xml);
        }
    }
}
Module Module1

    ''' <summary>
    ''' This code demonstrates the use of the following methods:
    '''     Engine constructor
    '''     Project constructor
    '''     Project.LoadFromXml
    '''     Project.Xml
    '''     BuildItemGroupCollection.GetEnumerator
    '''     BuildItemGroup.GetEnumerator
    '''     BuildItem.Name (get)
    '''     BuildItem.Include (set)
    '''     BuildItem.GetMetadata
    '''     BuildItem.SetMetadata
    '''     BuildItemGroup.RemoveItem
    '''     BuildItemGroup.AddNewItem
    ''' </summary>
    ''' <remarks></remarks>
    Sub Main()

        ' Create a new Engine object.
        Dim engine As New Engine(Environment.CurrentDirectory)

        ' Create a new Project object.
        Dim project As New Project(engine)

        ' Load the project with the following XML, which contains
        ' two ItemGroups.
        project.LoadXml( _
            "<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>" & _
                "<ItemGroup>" & _
                    "<Compile Include='Program.cs'/>" & _
                    "<Compile Include='Class1.cs'/>" & _
                    "<RemoveThisItemPlease Include='readme.txt'/>" & _
                "</ItemGroup>" & _
                "<ItemGroup>" & _
                    "<EmbeddedResource Include='Strings.resx'>" & _
                        "<LogicalName>Strings.resources</LogicalName>" & _
                        "<Culture>fr-fr</Culture>" & _
                    "</EmbeddedResource>" & _
                "</ItemGroup>" & _
            "</Project>" _
            )

        ' Iterate through each ItemGroup in the Project.  There are two.
        For Each ig As BuildItemGroup In project.ItemGroups

            Dim itemToRemove As BuildItem
            itemToRemove = Nothing

            ' Iterate through each Item in the ItemGroup.
            For Each item As BuildItem In ig

                ' If the item's name is "RemoveThisItemPlease", then
                ' store a reference to this item in a local variable,
                ' so we can remove it later.
                If item.Name = "RemoveThisItemPlease" Then
                    itemToRemove = item
                End If

                ' If the item's name is "EmbeddedResource" and it has a metadata Culture
                ' set to "fr-fr", then ...
                If (item.Name = "EmbeddedResource") And (item.GetMetadata("Culture") = "fr-fr") Then
                    ' Change the item's Include path to "FrenchStrings.fr.resx", 
                    ' and add a new metadata Visiable="false".
                    item.Include = "FrenchStrings.fr.resx"
                    item.SetMetadata("Visible", "false")
                End If
            Next

            ' Remove the item named "RemoveThisItemPlease" from the
            ' ItemGroup
            If Not itemToRemove Is Nothing Then
                ig.RemoveItem(itemToRemove)
            End If

            ' For each ItemGroup that we found, add to the end of it
            ' a new item Content with Include="SplashScreen.bmp".
            ig.AddNewItem("Content", "SplashScreen.bmp")
        Next

        ' The project now looks like this:
        '
        '     <?xml version="1.0" encoding="utf-16"?>
        '     <Project xmlns="http:'schemas.microsoft.com/developer/msbuild/2003">
        '         <Compile Include="Program.cs" />
        '         <Compile Include="Class1.cs" />
        '         <Content Include="SplashScreen.bmp" />
        '         <EmbeddedResource Include="FrenchStrings.fr.resx">
        '           <LogicalName>Strings.resources</LogicalName>
        '           <Culture>fr-fr</Culture>
        '           <Visible>false</Visible>
        '         <Content Include="SplashScreen.bmp" />
        '
        Console.WriteLine(project.Xml)

    End Sub

End Module

Commenti

Un elemento in un progetto può rappresentare più elementi tramite l'uso di caratteri jolly.An element in a project can represent multiple items through the use of wildcards. Ogni oggetto BuildItem non rappresenta pertanto necessariamente un elemento Item nel progetto.Therefore, each BuildItem object does not necessarily represent an Item element in the project.

Costruttori

BuildItem(String, ITaskItem) BuildItem(String, ITaskItem) BuildItem(String, ITaskItem) BuildItem(String, ITaskItem)

Inizializza una nuova istanza della classe BuildItem sulla base di un oggetto ITaskItem.Initializes a new instance of the BuildItem class based on an ITaskItem object.

BuildItem(String, String) BuildItem(String, String) BuildItem(String, String) BuildItem(String, String)

Inizializza una nuova istanza della classe BuildItem con i valori delle proprietà Name e Include specificati.Initializes a new instance of the BuildItem class with the specified Name and Include property values.

Proprietà

Condition Condition Condition Condition

Ottiene o imposta il valore dell'attributo Condition dell'elemento.Gets or sets the Condition attribute value of the item.

CustomMetadataCount CustomMetadataCount CustomMetadataCount CustomMetadataCount

Ottiene il numero di attributi personalizzati impostati su questo elemento.Gets the number of custom attributes that are set on this item.

CustomMetadataNames CustomMetadataNames CustomMetadataNames CustomMetadataNames

Ottiene tutti i nomi di attributo definiti dall'utente o personalizzati.Gets all user-defined or custom attribute names.

Exclude Exclude Exclude Exclude

Ottiene o imposta il valore dell'attributo Exclude dell'elemento.Gets or sets the Exclude attribute value of the item.

FinalItemSpec FinalItemSpec FinalItemSpec FinalItemSpec

Ottiene la specifica finale dell'elemento dopo la valutazione di tutti i caratteri jolly e le proprietà.Gets the final specification of the item after all wildcards and properties have been evaluated.

Include Include Include Include

Ottiene o imposta l'attributo Include che ha creato l'elemento BuildItem.Gets or sets the Include attribute that created the BuildItem.

IsImported IsImported IsImported IsImported

Ottiene un valore che indica se l'elemento è stato importato nel progetto.Gets a value indicating whether the item was imported into the project.

MetadataCount MetadataCount MetadataCount MetadataCount

Ottiene il numero dei metadati impostati nell'elemento.Gets the number of metadata set on the item.

MetadataNames MetadataNames MetadataNames MetadataNames

Ottiene i nomi dei metadati dell'elemento, inclusi i modificatori specifici di elemento predefiniti o riservati.Gets the names of metadata on the item, including pre-defined or reserved item-spec modifiers.

Name Name Name Name

Ottiene o imposta il nome dell'insieme di elementi a cui appartiene l'elemento.Gets or sets the name of the item collection to which the item belongs.

Metodi

Clone() Clone() Clone() Clone()

Crea una copia superficiale di BuildItem.Creates a shallow copy of the BuildItem.

CopyCustomMetadataTo(BuildItem) CopyCustomMetadataTo(BuildItem) CopyCustomMetadataTo(BuildItem) CopyCustomMetadataTo(BuildItem)

Copia tutti i metadati dell'elemento della classe BuildItem nella classe BuildItem specificata.Copies all item metadata on this BuildItem to the specified BuildItem.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEvaluatedMetadata(String) GetEvaluatedMetadata(String) GetEvaluatedMetadata(String) GetEvaluatedMetadata(String)

Restituisce il valore dei metadati dell'elemento specificati dopo la valutazione di tutti i riferimenti agli elementi e alle proprietà.Returns the value of the specified item metadata after all item and property references have been evaluated.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Funge da funzione hash predefinita.Serves as the default hash function.

(Inherited from Object)
GetMetadata(String) GetMetadata(String) GetMetadata(String) GetMetadata(String)

Restituisce il valore dei metadati dell'elemento specificati.Returns the value of the specified item metadata.

GetType() GetType() GetType() GetType()

Ottiene l'oggetto Type dell'istanza corrente.Gets the Type of the current instance.

(Inherited from Object)
HasMetadata(String) HasMetadata(String) HasMetadata(String) HasMetadata(String)

Indica se l'elemento possiede i metadati dell'elemento specificati.Indicates whether the item has the specified item metadata.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.Creates a shallow copy of the current Object.

(Inherited from Object)
RemoveMetadata(String) RemoveMetadata(String) RemoveMetadata(String) RemoveMetadata(String)

Rimuove i metadati dell'elemento specificati.Removes the specified item metadata.

SetMetadata(String, String) SetMetadata(String, String) SetMetadata(String, String) SetMetadata(String, String)

Assegna il valore specificato ai metadati dell'elemento specificati.Assigns the specified value to the specified item metadata.

SetMetadata(String, String, Boolean) SetMetadata(String, String, Boolean) SetMetadata(String, String, Boolean) SetMetadata(String, String, Boolean)

Assegna il valore specificato ai metadati dell'elemento specificati e, facoltativamente, considera i metadati come valore letterale.Assigns the specified value to the specified item metadata, and optionally treats the metadata as a literal value.

ToString() ToString() ToString() ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.Returns a string that represents the current object.

(Inherited from Object)

Si applica a