예제 #1
0
            public override ConstraintResult ApplyTo <TActual>(TActual actual)
            {
                string actualContract;

                switch (actual)
                {
                case string sourceCode:
                    actualContract = isVisualBasic
                            ? AssemblyUtils.GenerateContract(generator, sourceCode, Microsoft.CodeAnalysis.VisualBasic.LanguageVersion.Latest, assemblyResolver)
                            : AssemblyUtils.GenerateContract(generator, sourceCode, Microsoft.CodeAnalysis.CSharp.LanguageVersion.Latest, assemblyResolver);
                    break;

                case Compilation compilation:
                    using (var stream = new MemoryStream())
                    {
                        AssemblyUtils.EmitCompilation(compilation, stream);
                        stream.Seek(0, SeekOrigin.Begin);
                        actualContract = AssemblyUtils.GenerateContract(generator, stream, assemblyResolver);
                    }
                    break;

                case MetadataBuilder metadataBuilder:
                    var blobBuilder = new BlobBuilder();

                    var peBuilder = new ManagedPEBuilder(
                        PEHeaderBuilder.CreateLibraryHeader(),
                        new MetadataRootBuilder(metadataBuilder),
                        ilStream: new BlobBuilder());

                    peBuilder.Serialize(blobBuilder);

                    using (var stream = new MemoryStream(blobBuilder.ToArray(), writable: false))
                        actualContract = AssemblyUtils.GenerateContract(generator, stream, assemblyResolver);
                    break;

                default:
                    throw new ArgumentException("Expected source code as string or library IL as MetadataBuilder.", nameof(actual));
                }

                return(new ContractConstraintResult(this, actualContract, expected));
            }