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

时间:2015-06-27 18:36:37

标签: .net c#-4.0 codedom

我认为正在进行一些愚蠢的事情,因为其余的程序集级别属性可以包含在内,但只要声明AssemblyCopywriteAttributeAssemblyCompanyAttribute,就会导致CS0116和CS1730错误。鉴于代码不包含任何方法声明,我没有看到CS0116如何适用,并且没有散布的类型定义,因此不确定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);

1 个答案:

答案 0 :(得分:7)

错误是由文本中错误的分号引起的:

[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

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