通过 CodeDomProvider 使用 AssemblyCopyrightAttribute 或 AssemblyCompanyAttribute 时编译错误

Compile error when using AssemblyCopyrightAttribute or AssemblyCompanyAttribute via CodeDomProvider

我觉得有些愚蠢的事情正在发生,因为可以很好地包含剩余的程序集级属性,但是每当声明 AssemblyCopywriteAttributeAssemblyCompanyAttribute 时,它都会导致 CS0116 和 CS1730 错误。鉴于代码不包含任何方法声明,我看不出 CS0116 is applicable and there are no type definitions interspersed so not sure how CS1730 如何适用。

错误

Error Number: CS0116
Error Text: A namespace cannot directly contain members such as fields or methods

Error Number: CS1730
Error Text: Assembly and module attributes must precede all other elements defined in a file except using clauses and extern alias declarations

源文件:

using System;
using System.Reflection;
using System.Runtime.InteropServices;

[assembly: ComVisible(false)]
[assembly: CLSCompliant(false)]
[assembly: AssemblyCompany("My Company")]; // this results in a compile time error
[assembly: Guid("9d8271d9-957f-46dc-bcc6-1055137b4fad")]
[assembly: AssemblyTitle("CCDA MAP")]
[assembly: AssemblyDescription("The mapping logic to source a CXD and populate a CCDA")]
[assembly: AssemblyCopyright("My Company 2015")]; // this results in a compile time error
[assembly: AssemblyCulture("en-US")]
[assembly: AssemblyVersion("2.2.0")]
[assembly: AssemblyFileVersion("2.2.0.123")]
[assembly: AssemblyConfiguration("DEBUG")]
[assembly: AssemblyMetadataAttribute("Built","06/27/2015")]
[assembly: AssemblyMetadataAttribute("Host","JORMUNGANDR")]
[assembly: AssemblyMetadataAttribute("The answer","42")]
[assembly: AssemblyMetadataAttribute("Document Type","CCDA")]
[assembly: AssemblyMetadataAttribute("Document Spec Version","2.0")]

编译逻辑

CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");
var source = Directory.GetFiles(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),"codedom"),"*.cs").ToList().Dump("Map Source").Select(i=>File.ReadAllText(i)).ToArray();
var parameters = new CompilerParameters{ GenerateInMemory = true, OutputAssembly = string.Format("Map.dll",count),TreatWarningsAsErrors = true, WarningLevel = 4};
parameters.ReferencedAssemblies.Add("mscorlib.dll");
var results = provider.CompileAssemblyFromSource(parameters, source);

错误是由于文中的分号错误造成的:

[assembly: AssemblyCopyright("My Company 2015")]; // this results in a compile time error

应该是:

[assembly: AssemblyCopyright("My Company 2015")] // this does not result in a compile time error

并且:

[assembly: AssemblyCompany("My Company")]; // this results in a compile time error

应该是:

[assembly: AssemblyCompany("My Company")] // this does not result in a compile time error

删除它们会清除您看到的错误。