Пример #1
0
        public Checksum CreateChecksum(AnalyzerReference reference, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            using (var stream = SerializableBytes.CreateWritableStream())
                using (var writer = new ObjectWriter(stream, cancellationToken: cancellationToken))
                {
                    switch (reference)
                    {
                    case AnalyzerFileReference file:
                        WriteAnalyzerFileReferenceMvid(file, writer, cancellationToken);
                        break;

                    case UnresolvedAnalyzerReference unresolved:
                        WriteUnresolvedAnalyzerReferenceTo(unresolved, writer);
                        break;

                    case AnalyzerImageReference _:
                        // TODO: think a way to support this or a way to deal with this kind of situation.
                        // https://github.com/dotnet/roslyn/issues/15783
                        throw new NotSupportedException(nameof(AnalyzerImageReference));

                    default:
                        throw ExceptionUtilities.UnexpectedValue(reference.GetType());
                    }

                    stream.Position = 0;
                    return(Checksum.Create(stream));
                }
        }
        public void WriteTo(AnalyzerReference reference, ObjectWriter writer, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var file = reference as AnalyzerFileReference;

            if (file != null)
            {
                // fail to load analyzer assembly
                var assemblyPath = TryGetAnalyzerAssemblyPath(file);
                if (assemblyPath == null)
                {
                    WriteUnresolvedAnalyzerReferenceTo(reference, writer);
                    return;
                }

                writer.WriteString(nameof(AnalyzerFileReference));
                writer.WriteInt32((int)SerializationKinds.FilePath);

                writer.WriteString(file.FullPath);

                // TODO: remove this kind of host specific knowledge from common layer.
                //       but think moving it to host layer where this implementation detail actually exist.
                //
                // analyzer assembly path to load analyzer acts like
                // snapshot version for analyzer (since it is based on shadow copy)
                // we can't send over bits and load analyer from memory (image) due to CLR not being able
                // to find satellite dlls for analyzers.
                writer.WriteString(assemblyPath);
                return;
            }

            var unresolved = reference as UnresolvedAnalyzerReference;

            if (unresolved != null)
            {
                WriteUnresolvedAnalyzerReferenceTo(reference, writer);
                return;
            }

            var image = reference as AnalyzerImageReference;

            if (image != null)
            {
                // TODO: think a way to support this or a way to deal with this kind of situation.
                throw new NotSupportedException(nameof(AnalyzerImageReference));
            }

            throw ExceptionUtilities.UnexpectedValue(reference.GetType());
        }
Пример #3
0
        public void WriteTo(AnalyzerReference reference, ObjectWriter writer, bool usePathFromAssembly, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            switch (reference)
            {
            case AnalyzerFileReference file:
            {
                // fail to load analyzer assembly
                var assemblyPath = usePathFromAssembly ? TryGetAnalyzerAssemblyPath(file) : file.FullPath;
                if (assemblyPath == null)
                {
                    WriteUnresolvedAnalyzerReferenceTo(reference, writer);
                    return;
                }

                writer.WriteString(nameof(AnalyzerFileReference));
                writer.WriteInt32((int)SerializationKinds.FilePath);

                // TODO: remove this kind of host specific knowledge from common layer.
                //       but think moving it to host layer where this implementation detail actually exist.
                //
                // analyzer assembly path to load analyzer acts like
                // snapshot version for analyzer (since it is based on shadow copy)
                // we can't send over bits and load analyer from memory (image) due to CLR not being able
                // to find satellite dlls for analyzers.
                writer.WriteString(file.FullPath);
                writer.WriteString(assemblyPath);
                return;
            }

            case UnresolvedAnalyzerReference unresolved:
            {
                WriteUnresolvedAnalyzerReferenceTo(unresolved, writer);
                return;
            }

            case AnalyzerImageReference _:
            {
                // TODO: think a way to support this or a way to deal with this kind of situation.
                // https://github.com/dotnet/roslyn/issues/15783
                throw new NotSupportedException(nameof(AnalyzerImageReference));
            }

            default:
                throw ExceptionUtilities.UnexpectedValue(reference.GetType());
            }
        }