Пример #1
0
        public void CreateProjectInstanceFromProject()
        {
            const string CapturedMetadataName = "DefiningProjectFullPath";
            var          pc    = new ProjectCollection();
            var          projA = ProjectRootElement.Create(pc);
            var          projB = ProjectRootElement.Create(pc);

            projA.FullPath = Path.Combine(Path.GetTempPath(), "a.proj");
            projB.FullPath = Path.Combine(Path.GetTempPath(), "b.proj");
            projB.AddImport("a.proj");
            projA.AddItem("Compile", "aItem.cs");
            projB.AddItem("Compile", "bItem.cs");

            var loadSettings = ProjectLoadSettings.RecordDuplicateButNotCircularImports
                               | ProjectLoadSettings.RejectCircularImports
                               | ProjectLoadSettings.IgnoreEmptyImports
                               | ProjectLoadSettings.IgnoreMissingImports
                               | ProjectLoadSettings.IgnoreInvalidImports;

            var projBEval         = new Project(projB, null, null, pc, loadSettings);
            var projBInstance     = new ProjectInstance(projBEval, ProjectInstanceSettings.ImmutableWithFastItemLookup);
            var projBInstanceItem = projBInstance.GetItemsByItemTypeAndEvaluatedInclude("Compile", "bItem.cs").Single();
            var projAInstanceItem = projBInstance.GetItemsByItemTypeAndEvaluatedInclude("Compile", "aItem.cs").Single();

            Assert.Equal(projB.FullPath, projBInstanceItem.GetMetadataValue(CapturedMetadataName));
            Assert.Equal(projA.FullPath, projAInstanceItem.GetMetadataValue(CapturedMetadataName));

            // Although GetMetadataValue returns non-null, GetMetadata returns null...
            Assert.Null(projAInstanceItem.GetMetadata(CapturedMetadataName));

            // .. Just like built-in metadata does: (this segment just demonstrates similar functionality -- it's not meant to test built-in metadata)
            Assert.NotNull(projAInstanceItem.GetMetadataValue("Identity"));
            Assert.Null(projAInstanceItem.GetMetadata("Identity"));

            Assert.True(projAInstanceItem.HasMetadata(CapturedMetadataName));
            Assert.False(projAInstanceItem.Metadata.Any());
            Assert.Contains(CapturedMetadataName, projAInstanceItem.MetadataNames);
            Assert.Equal(projAInstanceItem.MetadataCount, projAInstanceItem.MetadataNames.Count);
        }