Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile error when using AssemblyCopyrightAttribute or AssemblyCompanyAttribute via CodeDomProvider

I figure something silly is going on as the remaining assembly level attributes can be included just fine but whenever AssemblyCopywriteAttribute or AssemblyCompanyAttribute is declared it results in CS0116 and CS1730 errors. Given that the code does not contain any method declarations I do not see how CS0116 is applicable and there are no type definitions interspersed so not sure how CS1730 is applicable.

Errors

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

Source File:

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")]

Compilation Logic

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);
like image 470
Tedford Avatar asked Jun 27 '15 18:06

Tedford


1 Answers

The error is caused by erroneous semicolons in the text:

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

Should be:

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

And:

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

Should be:

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

Removing them clears up the errors you're seeing.

like image 183
M.Babcock Avatar answered Oct 09 '22 07:10

M.Babcock