Compilation error when using AssemblyCopyrightAttribute or AssemblyCompanyAttribute via CodeDomProvider

I suppose something stupid happens, because the rest of the assembly-level attributes can be included just fine, but when AssemblyCopywriteAttribute or AssemblyCompanyAttribute declared, this leads to errors CS0116 and CS1730. Given that the code does not contain method declarations, I donโ€™t see how CS0116 is applicable, and there are no type definitions alternating, so I โ€™m not sure how CS1730 is .

Mistakes

 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 

Original 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); 
+6
source share
1 answer

The error is caused by erroneous semicolons in the text:

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

Must 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 

Must be:

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

Removing them clears the errors you see.

+7
source

Source: https://habr.com/ru/post/989865/


All Articles