Esempio n. 1
0
        public void TestMetadataImportOptions()
        {
            var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);

            Assert.Equal(MetadataImportOptions.Public, options.MetadataImportOptions);
            options = options.WithMetadataImportOptions(MetadataImportOptions.Internal);
            Assert.Equal(MetadataImportOptions.Internal, options.MetadataImportOptions);
            options = options.WithMetadataImportOptions(MetadataImportOptions.All);
            Assert.Equal(MetadataImportOptions.All, options.MetadataImportOptions);
            options = options.WithMetadataImportOptions(MetadataImportOptions.Public);
            Assert.Equal(MetadataImportOptions.Public, options.MetadataImportOptions);

            var commonOptions = (CompilationOptions)options;

            commonOptions = commonOptions.WithMetadataImportOptions(MetadataImportOptions.Internal);
            Assert.Equal(MetadataImportOptions.Internal, ((CSharpCompilationOptions)commonOptions).MetadataImportOptions);
            commonOptions = commonOptions.WithMetadataImportOptions(MetadataImportOptions.All);
            Assert.Equal(MetadataImportOptions.All, ((CSharpCompilationOptions)commonOptions).MetadataImportOptions);
            commonOptions = commonOptions.WithMetadataImportOptions(MetadataImportOptions.Public);
            Assert.Equal(MetadataImportOptions.Public, ((CSharpCompilationOptions)commonOptions).MetadataImportOptions);

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

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

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

            compilation = compilation.WithOptions(options.WithMetadataImportOptions(MetadataImportOptions.Internal));
            c           = compilation.GetTypeByMetadataName("C");
            Assert.NotEmpty(c.GetMembers("P1"));
            Assert.NotEmpty(c.GetMembers("P2"));
            Assert.Empty(c.GetMembers("P3"));

            compilation = compilation.WithOptions(options.WithMetadataImportOptions(MetadataImportOptions.All));
            c           = compilation.GetTypeByMetadataName("C");
            Assert.NotEmpty(c.GetMembers("P1"));
            Assert.NotEmpty(c.GetMembers("P2"));
            Assert.NotEmpty(c.GetMembers("P3"));
        }
        public static string Compile(string scriptPath, List <string> assemblyPaths)
        {
            var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true, optimizationLevel: OptimizationLevel.Debug);

            compilationOptions.WithMetadataImportOptions(MetadataImportOptions.All);
            typeof(CSharpCompilationOptions).GetProperty("TopLevelBinderFlags", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(compilationOptions, (uint)1 << 22);

            List <MetadataReference> metadataReferences = new List <MetadataReference>();

            foreach (var asmPath in assemblyPaths)
            {
                metadataReferences.Add(MetadataReference.CreateFromFile(asmPath));
            }

            var compilationResult = CSharpCompilation.Create("LatiosBurstPatched",
                                                             new[] { CSharpSyntaxTree.ParseText(File.ReadAllText(scriptPath)) },
                                                             metadataReferences,
                                                             compilationOptions).Emit(@"Library/PlayerScriptAssemblies/LatiosBurstPatched.dll");

            // Output compile errors.
            foreach (var d in compilationResult.Diagnostics.Where(d => d.IsWarningAsError || d.Severity == DiagnosticSeverity.Error))
            {
                Debug.LogError(string.Format("{0} ({1}): {2} {3}", d.Severity, d.Id, d.GetMessage(), d.Location.GetMappedLineSpan()));
            }

            return(compilationResult.Success ? @"Library/PlayerScriptAssemblies/LatiosBurstPatched.dll" : null);
        }
Esempio n. 3
0
        public void TestMetadataImportOptions_01()
        {
            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);

            Assert.Equal(MetadataImportOptions.Public, options.MetadataImportOptions);
            options.VerifyErrors();
            options = options.WithMetadataImportOptions(MetadataImportOptions.Internal);
            Assert.Equal(MetadataImportOptions.Internal, options.MetadataImportOptions);
            options.VerifyErrors();
            options = options.WithMetadataImportOptions(MetadataImportOptions.All);
            Assert.Equal(MetadataImportOptions.All, options.MetadataImportOptions);
            options.VerifyErrors();
            options = options.WithMetadataImportOptions(MetadataImportOptions.Public);
            Assert.Equal(MetadataImportOptions.Public, options.MetadataImportOptions);
            options.VerifyErrors();
            options = options.WithMetadataImportOptions((MetadataImportOptions)byte.MaxValue);
            Assert.Equal((MetadataImportOptions)byte.MaxValue, options.MetadataImportOptions);
            options.VerifyErrors(expectedDiagnostics);

            var commonOptions = (CompilationOptions)options;

            commonOptions = commonOptions.WithMetadataImportOptions(MetadataImportOptions.Internal);
            Assert.Equal(MetadataImportOptions.Internal, ((CSharpCompilationOptions)commonOptions).MetadataImportOptions);
            ((CSharpCompilationOptions)commonOptions).VerifyErrors();
            commonOptions = commonOptions.WithMetadataImportOptions(MetadataImportOptions.All);
            Assert.Equal(MetadataImportOptions.All, ((CSharpCompilationOptions)commonOptions).MetadataImportOptions);
            ((CSharpCompilationOptions)commonOptions).VerifyErrors();
            commonOptions = commonOptions.WithMetadataImportOptions(MetadataImportOptions.Public);
            Assert.Equal(MetadataImportOptions.Public, ((CSharpCompilationOptions)commonOptions).MetadataImportOptions);
            ((CSharpCompilationOptions)commonOptions).VerifyErrors();
            commonOptions = commonOptions.WithMetadataImportOptions((MetadataImportOptions)byte.MaxValue);
            Assert.Equal((MetadataImportOptions)byte.MaxValue, ((CSharpCompilationOptions)commonOptions).MetadataImportOptions);
            ((CSharpCompilationOptions)commonOptions).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);

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

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

            compilation = compilation.WithOptions(options.WithMetadataImportOptions(MetadataImportOptions.Internal));
            c           = compilation.GetTypeByMetadataName("C");
            Assert.NotEmpty(c.GetMembers("P1"));
            Assert.NotEmpty(c.GetMembers("P2"));
            Assert.Empty(c.GetMembers("P3"));
            CompileAndVerify(compilation);

            compilation = compilation.WithOptions(options.WithMetadataImportOptions(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(options.WithMetadataImportOptions((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);
        }