Пример #1
0
        public void TestMetadataImportOptions_02()
        {
            var expectedDiagnostics = new[]
            {
                // error CS7088: Invalid 'MetadataImportOptions' value: '255'.
                Diagnostic(ErrorCode.ERR_BadCompilationOptionValue).WithArguments("MetadataImportOptions", "255").WithLocation(1, 1)
            };

            var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, metadataImportOptions: MetadataImportOptions.Internal);

            Assert.Equal(MetadataImportOptions.Internal, options.MetadataImportOptions);
            options.VerifyErrors();
            options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, metadataImportOptions: MetadataImportOptions.All);
            Assert.Equal(MetadataImportOptions.All, options.MetadataImportOptions);
            options.VerifyErrors();
            options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, metadataImportOptions: MetadataImportOptions.Public);
            Assert.Equal(MetadataImportOptions.Public, options.MetadataImportOptions);
            options.VerifyErrors();
            options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, metadataImportOptions: (MetadataImportOptions)byte.MaxValue);
            Assert.Equal((MetadataImportOptions)byte.MaxValue, options.MetadataImportOptions);
            options.VerifyErrors(expectedDiagnostics);

            var source       = @"
public class C
{
    public int P1 {get; set;}
    internal int P2 {get; set;}
    private int P3 {get; set;}
}
";
            var compilation0 = CreateCompilation(source);

            var compilation = CreateCompilation("", options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, metadataImportOptions: MetadataImportOptions.Internal), references: new[] { compilation0.EmitToImageReference() });
            var c           = compilation.GetTypeByMetadataName("C");

            Assert.NotEmpty(c.GetMembers("P1"));
            Assert.NotEmpty(c.GetMembers("P2"));
            Assert.Empty(c.GetMembers("P3"));
            CompileAndVerify(compilation);

            compilation = compilation.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, metadataImportOptions: MetadataImportOptions.All));
            c           = compilation.GetTypeByMetadataName("C");
            Assert.NotEmpty(c.GetMembers("P1"));
            Assert.NotEmpty(c.GetMembers("P2"));
            Assert.NotEmpty(c.GetMembers("P3"));
            CompileAndVerify(compilation);

            compilation = compilation.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, metadataImportOptions: (MetadataImportOptions)byte.MaxValue));
            c           = compilation.GetTypeByMetadataName("C");
            Assert.NotEmpty(c.GetMembers("P1"));
            Assert.NotEmpty(c.GetMembers("P2"));
            Assert.Empty(c.GetMembers("P3"));
            compilation.VerifyEmitDiagnostics(expectedDiagnostics);
            compilation.VerifyDiagnostics(expectedDiagnostics);
        }