public void WithXxx()
        {
            var p = new MetadataReferenceProperties(MetadataImageKind.Assembly, ImmutableArray.Create("a"), embedInteropTypes: false);

            Assert.Equal(p.WithAliases(null), new MetadataReferenceProperties(MetadataImageKind.Assembly, ImmutableArray<string>.Empty, embedInteropTypes: false));
            Assert.Equal(p.WithAliases(default(ImmutableArray<string>)), new MetadataReferenceProperties(MetadataImageKind.Assembly, ImmutableArray<string>.Empty, embedInteropTypes: false));
            Assert.Equal(p.WithAliases(ImmutableArray<string>.Empty), new MetadataReferenceProperties(MetadataImageKind.Assembly, default(ImmutableArray<string>), embedInteropTypes: false));
            Assert.Equal(p.WithAliases(new string[0]), new MetadataReferenceProperties(MetadataImageKind.Assembly, default(ImmutableArray<string>), embedInteropTypes: false));
            Assert.Equal(p.WithAliases(new[] { "foo", "aaa" }), new MetadataReferenceProperties(MetadataImageKind.Assembly, ImmutableArray.Create("foo", "aaa"), embedInteropTypes: false));
            Assert.Equal(p.WithEmbedInteropTypes(true), new MetadataReferenceProperties(MetadataImageKind.Assembly, ImmutableArray.Create("a"), embedInteropTypes: true));

            var m = new MetadataReferenceProperties(MetadataImageKind.Module);
            Assert.Equal(m.WithAliases(default(ImmutableArray<string>)), new MetadataReferenceProperties(MetadataImageKind.Module, default(ImmutableArray<string>), embedInteropTypes: false));
            Assert.Equal(m.WithEmbedInteropTypes(false), new MetadataReferenceProperties(MetadataImageKind.Module, default(ImmutableArray<string>), embedInteropTypes: false));
        }
Exemple #2
0
        private void RemoveFromContextIfPresent(string fullPath, MetadataReferenceProperties properties, IProjectLogger logger)
        {
            if (_addedPathsWithMetadata.TryGetValue(fullPath, out var existingProperties))
            {
                logger.WriteLine("Removing {0} '{1}'", properties.EmbedInteropTypes ? "link" : "reference", fullPath);

                _context.RemoveMetadataReference(fullPath);

                // Subtract any existing aliases out. This will be an empty list if we should remove the reference entirely
                var resultantAliases = GetGlobalAliasIfEmpty(existingProperties.Aliases).RemoveRange(GetGlobalAliasIfEmpty(properties.Aliases));

                if (resultantAliases.IsEmpty)
                {
                    // There's nothing left here, completely remove it
                    Assumes.True(_addedPathsWithMetadata.Remove(fullPath));
                }
                else
                {
                    // resultantAliases might be the global alias. In that case, let's remove it again.
                    resultantAliases = GetEmptyIfGlobalAlias(resultantAliases);
                    properties       = properties.WithAliases(resultantAliases);
                    logger.WriteLine("Adding {0} '{1}' back with remaining aliases", properties.EmbedInteropTypes ? "link" : "reference", fullPath);
                    _context.AddMetadataReference(fullPath, properties);
                    _addedPathsWithMetadata[fullPath] = properties;
                }
            }
        }
        public void WithXxx()
        {
            var p = new MetadataReferenceProperties(MetadataImageKind.Assembly, ImmutableArray.Create("a"), embedInteropTypes: false);

            Assert.Equal(p.WithAliases(null), new MetadataReferenceProperties(MetadataImageKind.Assembly, ImmutableArray <string> .Empty, embedInteropTypes: false));
            Assert.Equal(p.WithAliases(default(ImmutableArray <string>)), new MetadataReferenceProperties(MetadataImageKind.Assembly, ImmutableArray <string> .Empty, embedInteropTypes: false));
            Assert.Equal(p.WithAliases(ImmutableArray <string> .Empty), new MetadataReferenceProperties(MetadataImageKind.Assembly, default(ImmutableArray <string>), embedInteropTypes: false));
            Assert.Equal(p.WithAliases(new string[0]), new MetadataReferenceProperties(MetadataImageKind.Assembly, default(ImmutableArray <string>), embedInteropTypes: false));
            Assert.Equal(p.WithAliases(new[] { "foo", "aaa" }), new MetadataReferenceProperties(MetadataImageKind.Assembly, ImmutableArray.Create("foo", "aaa"), embedInteropTypes: false));
            Assert.Equal(p.WithEmbedInteropTypes(true), new MetadataReferenceProperties(MetadataImageKind.Assembly, ImmutableArray.Create("a"), embedInteropTypes: true));

            var m = new MetadataReferenceProperties(MetadataImageKind.Module);

            Assert.Equal(m.WithAliases(default(ImmutableArray <string>)), new MetadataReferenceProperties(MetadataImageKind.Module, default(ImmutableArray <string>), embedInteropTypes: false));
            Assert.Equal(m.WithEmbedInteropTypes(false), new MetadataReferenceProperties(MetadataImageKind.Module, default(ImmutableArray <string>), embedInteropTypes: false));
        }
        private void AddToContextIfNotPresent(string fullPath, MetadataReferenceProperties properties, IProjectLogger logger)
        {
            if (_addedPathsWithMetadata.TryGetValue(fullPath, out var existingProperties))
            {
                logger.WriteLine("Removing existing reference with '{0}' so we can update the aliases.", fullPath);

                // The reference has already been added previously. The current implementation of IWorkspaceProjectContext
                // presumes that we'll only called AddMetadataReference once for a given path. Thus we have to remove the
                // existing one, compute merged properties, and add the new one.
                _context.RemoveMetadataReference(fullPath);

                var combinedAliases = GetEmptyIfGlobalAlias(GetGlobalAliasIfEmpty(existingProperties.Aliases).AddRange(GetGlobalAliasIfEmpty(properties.Aliases)));
                properties = properties.WithAliases(combinedAliases);
            }

            logger.WriteLine("Adding reference '{0}'", fullPath);
            _context.AddMetadataReference(fullPath, properties);
            _addedPathsWithMetadata[fullPath] = properties;
        }