public static CSharpCompilationOptions CreateCompilationOptions(this ProjectFileInfo projectFileInfo) { var compilationOptions = new CSharpCompilationOptions(projectFileInfo.OutputKind); compilationOptions = compilationOptions.WithAssemblyIdentityComparer(DesktopAssemblyIdentityComparer.Default) .WithSpecificDiagnosticOptions(projectFileInfo.GetDiagnosticOptions()) .WithOverflowChecks(projectFileInfo.CheckForOverflowUnderflow); if (projectFileInfo.AllowUnsafeCode) { compilationOptions = compilationOptions.WithAllowUnsafe(true); } if (projectFileInfo.TreatWarningsAsErrors) { compilationOptions = compilationOptions.WithGeneralDiagnosticOption(ReportDiagnostic.Error); } if (projectFileInfo.NullableContextOptions != compilationOptions.NullableContextOptions) { compilationOptions = compilationOptions.WithNullableContextOptions(projectFileInfo.NullableContextOptions); } if (projectFileInfo.SignAssembly && !string.IsNullOrEmpty(projectFileInfo.AssemblyOriginatorKeyFile)) { var keyFile = Path.Combine(projectFileInfo.Directory, projectFileInfo.AssemblyOriginatorKeyFile); compilationOptions = compilationOptions.WithStrongNameProvider(new DesktopStrongNameProvider()) .WithCryptoKeyFile(keyFile); } if (!string.IsNullOrWhiteSpace(projectFileInfo.DocumentationFile)) { compilationOptions = compilationOptions.WithXmlReferenceResolver(XmlFileResolver.Default); } return(compilationOptions); }
public void WithNullable() { CSharpCompilationOptions a = CreateCSharpCompilationOptions(); Assert.Equal(NullableContextOptions.Disabled, a.NullableContextOptions); Assert.Equal(a, a); Assert.Equal(a.GetHashCode(), a.GetHashCode()); Assert.Same(a, a.WithNullableContextOptions(NullableContextOptions.Disabled)); CSharpCompilationOptions b = a.WithNullableContextOptions(NullableContextOptions.Enabled); Assert.Equal(NullableContextOptions.Enabled, b.NullableContextOptions); Assert.NotEqual(a, b); Assert.Equal(b, b); Assert.Equal(b.GetHashCode(), b.GetHashCode()); Assert.Same(b, b.WithNullableContextOptions(NullableContextOptions.Enabled)); CSharpCompilationOptions c = a.WithNullableContextOptions(NullableContextOptions.Enabled); Assert.Equal(NullableContextOptions.Enabled, c.NullableContextOptions); Assert.NotEqual(a, c); Assert.NotSame(b, c); Assert.Equal(b, c); Assert.Equal(b.GetHashCode(), c.GetHashCode()); CSharpCompilationOptions e = b.WithNullableContextOptions(NullableContextOptions.Disabled); Assert.Equal(NullableContextOptions.Disabled, e.NullableContextOptions); Assert.NotSame(a, e); Assert.Equal(a, e); Assert.Equal(a.GetHashCode(), e.GetHashCode()); Assert.NotEqual(b, e); Assert.NotEqual(c, e); CSharpCompilationOptions d = a.WithNullableContextOptions(NullableContextOptions.SafeOnly); Assert.Equal(NullableContextOptions.SafeOnly, d.NullableContextOptions); Assert.NotEqual(a, d); Assert.NotEqual(b, d); Assert.NotEqual(c, d); Assert.Equal(d, d); Assert.Equal(d.GetHashCode(), d.GetHashCode()); Assert.Same(d, d.WithNullableContextOptions(NullableContextOptions.SafeOnly)); CSharpCompilationOptions f = b.WithNullableContextOptions(NullableContextOptions.SafeOnly); Assert.Equal(NullableContextOptions.SafeOnly, f.NullableContextOptions); Assert.NotEqual(a, f); Assert.NotEqual(b, f); Assert.NotEqual(c, f); Assert.NotSame(d, f); Assert.Equal(d, f); Assert.Equal(d.GetHashCode(), f.GetHashCode()); CSharpCompilationOptions g = d.WithNullableContextOptions(NullableContextOptions.Enabled); Assert.Equal(NullableContextOptions.Enabled, g.NullableContextOptions); Assert.NotEqual(a, g); Assert.NotSame(b, g); Assert.Equal(b, g); Assert.Equal(b.GetHashCode(), g.GetHashCode()); Assert.NotEqual(d, g); CSharpCompilationOptions h = d.WithNullableContextOptions(NullableContextOptions.Disabled); Assert.Equal(NullableContextOptions.Disabled, h.NullableContextOptions); Assert.Equal(a, h); Assert.Equal(a.GetHashCode(), h.GetHashCode()); Assert.NotEqual(b, h); Assert.NotEqual(c, h); Assert.NotEqual(d, h); var i = new CSharpCompilationOptions(OutputKind.ConsoleApplication); Assert.Equal(NullableContextOptions.Disabled, i.NullableContextOptions); var j = new CSharpCompilationOptions(OutputKind.ConsoleApplication, nullableContextOptions: NullableContextOptions.Enabled); Assert.Equal(NullableContextOptions.Enabled, j.NullableContextOptions); var k = new CSharpCompilationOptions(OutputKind.ConsoleApplication, nullableContextOptions: NullableContextOptions.Disabled); Assert.Equal(NullableContextOptions.Disabled, k.NullableContextOptions); var l = new CSharpCompilationOptions(OutputKind.ConsoleApplication, nullableContextOptions: NullableContextOptions.SafeOnly); Assert.Equal(NullableContextOptions.SafeOnly, l.NullableContextOptions); }