本文整理汇总了C#中Microsoft.Build.Evaluation.Project.GetItems方法的典型用法代码示例。如果您正苦于以下问题:C# Project.GetItems方法的具体用法?C# Project.GetItems怎么用?C# Project.GetItems使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Build.Evaluation.Project
的用法示例。
在下文中一共展示了Project.GetItems方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Project
public Project(Solution solution, string title, string fileName)
{
AssembliesResolved = false;
ReferencedAssemblies = new List<string>();
CompilerSettings = new CompilerSettings();
ReferencedProjects = new List<string>();
Files = new List<File>();
Solution = solution;
Title = title;
FileName = Path.GetFullPath(fileName);
ProjectCollection.GlobalProjectCollection.UnloadAllProjects();
MsBuildProject = new Microsoft.Build.Evaluation.Project(fileName);
AssemblyName = MsBuildProject.GetPropertyValue("AssemblyName");
CompilerSettings.AllowUnsafeBlocks = MsBuildProject.GetPropertyAsBoolean("AllowUnsafeBlocks");
CompilerSettings.CheckForOverflow = MsBuildProject.GetPropertyAsBoolean("CheckForOverflowUnderflow");
var defineConstants = MsBuildProject.GetPropertyValue("DefineConstants");
foreach (string symbol in defineConstants.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
{
CompilerSettings.ConditionalSymbols.Add(symbol.Trim());
}
foreach (var sourceCodeFile in MsBuildProject.GetItems("Compile"))
{
Files.Add(new File(this, Path.Combine(MsBuildProject.DirectoryPath, sourceCodeFile.EvaluatedInclude)));
}
foreach (var projectReference in MsBuildProject.GetItems("ProjectReference"))
{
string referencedFileName = Path.GetFullPath(Path.Combine(MsBuildProject.DirectoryPath, projectReference.EvaluatedInclude));
ReferencedProjects.Add(referencedFileName);
}
}
示例2: CreateCompilation
private Compilation CreateCompilation(Project project)
{
var outputPath = project.GetProperty("OutputPath").EvaluatedValue;
if (!Path.IsPathRooted(outputPath))
{
outputPath = Path.Combine(Environment.CurrentDirectory, outputPath);
}
var searchPaths = ReadOnlyArray.OneOrZero(outputPath);
var resolver = new DiskFileResolver(searchPaths, searchPaths, Environment.CurrentDirectory, arch => true, System.Globalization.CultureInfo.CurrentCulture);
var metadataFileProvider = new MetadataFileProvider();
// just grab a list of references (if they're null, ignore)
var list = project.GetItems("Reference").Select(item =>
{
var include = item.EvaluatedInclude;
var path = resolver.ResolveAssemblyName(include);
if (path == null) return null;
return metadataFileProvider.GetReference(path);
}).Where(x => x != null);
return Compilation.Create(project.GetPropertyValue("AssemblyName"),
syntaxTrees: project.GetItems("Compile").Select(c => SyntaxTree.ParseFile(c.EvaluatedInclude)),
references: list);
}
示例3: SortProject
private IEnumerable<Project> SortProject(Project project)
{
if (!visited.Contains(project)) {
visited.Add(project);
foreach (var reference in project.GetItems("Reference")) {
//TODO: Get rid of hard-coded exclusions
if (reference.EvaluatedInclude != "DevExpress.XtraRichEdit.v12.2.Extensions, Version=12.2.0.0, Culture=neutral, PublicKeyToken=79868b8147b5eae4, processorArchitecture=MSIL") {
AssemblyName assemblyName = new AssemblyName(reference.EvaluatedInclude);
string shortName = assemblyName.Name;
Project referencedProject;
if (projects.TryGetValue(shortName, out referencedProject)) {
foreach (var sorted in SortProject(referencedProject))
yield return sorted;
}
else if (excludedProjects.Contains(shortName)) {
excludedProjects.Add(shortName);
yield break;
}
else if (shortName.StartsWith("DevExpress", StringComparison.OrdinalIgnoreCase))
unknownReferences.Add(shortName);
}
}
yield return project;
}
}
示例4: SimpleImportAndSemanticValues
public void SimpleImportAndSemanticValues ()
{
string xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
<Import Project='test_imported.proj' />
</Project>";
string imported = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
<PropertyGroup>
<A>x</A>
<B>y</B>
</PropertyGroup>
<ItemGroup>
<X Include=""included.txt"" />
</ItemGroup>
</Project>";
using (var ts = File.CreateText (temp_file_name))
ts.Write (imported);
try {
var reader = XmlReader.Create (new StringReader (xml));
var root = ProjectRootElement.Create (reader);
Assert.AreEqual (temp_file_name, root.Imports.First ().Project, "#1");
var proj = new Project (root);
var prop = proj.GetProperty ("A");
Assert.IsNotNull (prop, "#2");
Assert.IsTrue (prop.IsImported, "#3");
var item = proj.GetItems ("X").FirstOrDefault ();
Assert.IsNotNull (item, "#4");
Assert.AreEqual ("included.txt", item.EvaluatedInclude, "#5");
} finally {
File.Delete (temp_file_name);
}
}
示例5: PackageContainsContentItemsAsync
public static async Task PackageContainsContentItemsAsync(Project nuProj)
{
var package = await nuProj.GetPackageAsync();
foreach (var contentItem in nuProj.GetItems("Content"))
{
var expectedPath = GetExpectedPackagePathForContent(contentItem);
Assert.NotNull(package.GetFile(expectedPath));
}
}
示例6: SetUnevaluatedValue
public void SetUnevaluatedValue()
{
string content = ObjectModelHelpers.CleanupFileContents(@"
<Project xmlns='msbuildnamespace' >
<ItemGroup>
<i Include='i1'>
<m1>v1</m1>
<m2>v%253</m2>
</i>
</ItemGroup>
</Project>
");
ProjectRootElement projectXml = ProjectRootElement.Create(XmlReader.Create(new StringReader(content)));
Project project = new Project(projectXml);
Assert.AreEqual(false, project.IsDirty);
Helpers.GetFirst(project.GetItems("i")).SetMetadataValue("m1", "v2");
Helpers.GetFirst(project.GetItems("i")).SetMetadataValue("m2", "v%214");
Assert.AreEqual(true, project.IsDirty);
StringWriter writer = new StringWriter();
projectXml.Save(writer);
string expected = ObjectModelHelpers.CleanupFileContents(@"
<Project xmlns='msbuildnamespace' >
<ItemGroup>
<i Include='i1'>
<m1>v2</m1>
<m2>v%214</m2>
</i>
</ItemGroup>
</Project>
");
Helpers.CompareProjectXml(expected, writer.ToString());
Assert.AreEqual("v!4", Helpers.GetFirst(project.GetItems("i")).GetMetadataValue("m2"));
}
示例7: SetMetadata
public void SetMetadata()
{
ProjectRootElement xml = ProjectRootElement.Create();
xml.AddProperty("p", "v");
xml.AddItemDefinitionGroup().AddItemDefinition("i").AddMetadata("m", "m0");
xml.AddItem("i", "i1");
Project project = new Project(xml);
ProjectMetadata metadatum = project.ItemDefinitions["i"].GetMetadata("m");
metadatum.UnevaluatedValue = "$(p)";
Assert.Equal("v", Helpers.GetFirst(project.GetItems("i")).GetMetadataValue("m"));
}
示例8: GetProjectReferences
static IEnumerable<Project> GetProjectReferences(Project project)
{
foreach (var reference in project.GetItems("ProjectReference"))
{
var referenced = new Project(
Path.Combine(Path.GetDirectoryName(project.FullPath), reference.EvaluatedInclude),
project.GlobalProperties,
project.ToolsVersion,
new ProjectCollection(project.GlobalProperties));
yield return referenced;
foreach (var transitive in GetProjectReferences(referenced))
{
yield return transitive;
}
}
}
示例9: AddItem_EscapedItemInclude
public void AddItem_EscapedItemInclude()
{
Project project = new Project();
project.AddItem("i", "i%281%29");
string expected = ObjectModelHelpers.CleanupFileContents(
@"<Project ToolsVersion=""msbuilddefaulttoolsversion"" xmlns=""msbuildnamespace"">
<ItemGroup>
<i Include=""i%281%29"" />
</ItemGroup>
</Project>");
Helpers.VerifyAssertProjectContent(expected, project.Xml);
List<ProjectItem> items = Helpers.MakeList(project.Items);
Assert.Equal(1, items.Count);
Assert.Equal("i", items[0].ItemType);
Assert.Equal("i(1)", items[0].EvaluatedInclude);
Assert.Equal("i(1)", Helpers.GetFirst(project.GetItems("i")).EvaluatedInclude);
Assert.Equal("i(1)", Helpers.MakeList(project.CreateProjectInstance().GetItems("i"))[0].EvaluatedInclude);
}
示例10: AddAllProjectReferences
private static IProjectContent AddAllProjectReferences(Project msbuildProject, IProjectContent pc)
{
foreach (var item in msbuildProject.GetItems("ProjectReference"))
{
var referencedFileName = Path.Combine(msbuildProject.DirectoryPath, item.EvaluatedInclude);
referencedFileName = Path.GetFullPath(referencedFileName);
pc = pc.AddAssemblyReferences(new[] {new ProjectReference(referencedFileName)});
}
return pc;
}
示例11: GetImportedItem
/// <summary>
/// Gets the test item from the import.
/// </summary>
/// <param name="project">The project.</param>
/// <returns>The item.</returns>
private ProjectItem GetImportedItem(Project project)
{
IEnumerable<ProjectItem> items = project.GetItems(ItemType).Where(pi => pi.IsImported);
Assert.IsTrue(items.Count() == 1, "Wrong number of items in the import.");
ProjectItem item = items.First();
Assert.AreEqual(importFilename, item.Xml.ContainingProject.FullPath, "Item was not found in the imported project.");
return item;
}
示例12: CanGetCorrectListOfItemsWithSemicolonsInThem2
public void CanGetCorrectListOfItemsWithSemicolonsInThem2()
{
string projectString = @"
<Project DefaultTargets=""Build"" ToolsVersion=""msbuilddefaulttoolsversion"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
<PropertyGroup>
<MyUserMacro>foo;bar</MyUserMacro>
</PropertyGroup>
<ItemGroup>
<CrazyList Include=""a"" />
<CrazyList Include=""b%3bc"" />
<CrazyList Include=""$(MyUserMacro)"" />
</ItemGroup>
</Project>";
System.Xml.XmlReader reader = new System.Xml.XmlTextReader(new StringReader(projectString));
Project project = new Project(reader);
IEnumerable<ProjectItem> items = project.GetItems("CrazyList");
Assert.Equal(4, items.Count());
Assert.Equal(items.ElementAt(0).EvaluatedInclude, "a");
Assert.Equal(items.ElementAt(1).EvaluatedInclude, "b;c");
Assert.Equal(items.ElementAt(2).EvaluatedInclude, "foo");
Assert.Equal(items.ElementAt(3).EvaluatedInclude, "bar");
}
示例13: ModifyItemOfTypeInProject
/// <summary>
/// Given a project and an item type, gets the items of that type, and renames an item
/// with the old evaluated include to have the new evaluated include instead.
/// </summary>
/// <param name="project"></param>
/// <param name="itemType"></param>
/// <param name="oldEvaluatedInclude"></param>
/// <param name="newEvaluatedInclude"></param>
internal static IEnumerable<ProjectItem> ModifyItemOfTypeInProject(Project project, string itemType, string oldEvaluatedInclude, string newEvaluatedInclude)
{
IEnumerable<ProjectItem> itemsToMatch = project.GetItems(itemType);
List<ProjectItem> matchingItems = new List<ProjectItem>();
foreach (ProjectItem item in itemsToMatch)
{
if (String.Equals(item.EvaluatedInclude, oldEvaluatedInclude, StringComparison.OrdinalIgnoreCase))
{
matchingItems.Add(item);
}
}
for (int i = 0; i < matchingItems.Count; i++)
{
matchingItems[i].Rename(newEvaluatedInclude);
}
return matchingItems;
}
示例14: TransformsUseCorrectDirectory_DirectoryNameItemFunction
public void TransformsUseCorrectDirectory_DirectoryNameItemFunction()
{
string file = null;
string projectFileContent = ObjectModelHelpers.CleanupFileContents(@"
<Project xmlns='msbuildnamespace'>
<ItemGroup>
<IntermediateAssembly Include='obj\i386\foo.dll'/>
<BuiltProjectOutputGroupKeyOutput Include=""@(IntermediateAssembly->DirectoryName())""/>
</ItemGroup>
</Project>");
ProjectRootElement xml = ProjectRootElement.Create(XmlReader.Create(new StringReader(projectFileContent)));
try
{
file = Microsoft.Build.Shared.FileUtilities.GetTemporaryFile();
xml.FullPath = file;
Project project = new Project(xml);
ProjectInstance projectInstance = new ProjectInstance(xml);
// Should be the full path to the directory
Assert.Equal(Path.Combine(Path.GetTempPath() /* remove c:\ */, @"obj\i386"), project.GetItems("BuiltProjectOutputGroupKeyOutput").First().EvaluatedInclude);
Assert.Equal(Path.Combine(Path.GetTempPath() /* remove c:\ */, @"obj\i386"), projectInstance.GetItems("BuiltProjectOutputGroupKeyOutput").First().EvaluatedInclude);
}
finally
{
File.Delete(file);
}
}
示例15: TransformsUseCorrectDirectory_Basic
public void TransformsUseCorrectDirectory_Basic()
{
string file = null;
string projectFileContent = ObjectModelHelpers.CleanupFileContents(@"
<Project xmlns='msbuildnamespace'>
<ItemGroup>
<IntermediateAssembly Include='obj\i386\foo.dll'/>
<BuiltProjectOutputGroupKeyOutput Include=""@(IntermediateAssembly->'%(FullPath)')""/>
</ItemGroup>
</Project>");
ProjectRootElement xml = ProjectRootElement.Create(XmlReader.Create(new StringReader(projectFileContent)));
Project project = new Project(xml);
try
{
file = Microsoft.Build.Shared.FileUtilities.GetTemporaryFile();
project.Save(file);
project.ReevaluateIfNecessary();
Assert.Equal(Path.Combine(Path.GetTempPath(), @"obj\i386\foo.dll"), project.GetItems("BuiltProjectOutputGroupKeyOutput").First().EvaluatedInclude);
}
finally
{
File.Delete(file);
}
}