[Mono-dev] CSharpCodProvider problems
noobody
gniall at gnoware.com
Tue Apr 27 20:59:44 EDT 2010
Hello all,
New to Mono, finding it grand in some spots and problematic in others:
mainly the CodeDom stuff, as my switching over to Mono depends on getting
this running smoothly...
Basically just trying to compile source code from a file. Basing the code
from Microsoft's own .NET dev site...
But it doesn't work. No matter what i do i keep getting this error:
error CS0116: A namespace does not directly contain members such as fields
or methods
Here is the function that reads the file (below is the actual file text,
saved as a UTF-8 .cs file). Function seems to open/read the file (i get no
IO errors), but no matter how i rearrange the source code, i keep getting
that above error.
///// The Source Function...
public bool CompileCSharpCode (String sourceFile, String exeFile)
{
// Obtain an ICodeCompiler from a CodeDomProvider class.
CSharpCodeProvider provider = new CSharpCodeProvider();
/// Obsolete ! [this is changed from the MS example]...
// ICodeCompiler compiler = provider.CreateCompiler();
// Build the parameters for source compilation.
CompilerParameters cp = new CompilerParameters();
// Add an assembly reference.
cp.ReferencedAssemblies.Add( "System.dll" );
// Generate an executable instead of
// a class library.
cp.GenerateExecutable = true;
// Set the assembly file name to generate.
cp.OutputAssembly = exeFile;
// Save the assembly as a physical file.
cp.GenerateInMemory = false;
// Invoke compilation. // Changed from the old ICodeCompiler to use
provider directly!
CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceFile);
Console.WriteLine("Errors count = " + cr.Errors.Count.ToString());
if(cr.Errors.Count > 0)
{
// Display compilation errors.
Console.WriteLine("Errors building {0} into {1}",
sourceFile, cr.PathToAssembly);
foreach(CompilerError ce in cr.Errors)
{
Console.WriteLine(" {0}", ce.ToString());
Console.WriteLine();
}
}
else
{
Console.WriteLine("Source {0} built into {1} successfully.",
sourceFile, cr.PathToAssembly);
}
// Return the results of compilation.
if (cr.Errors.Count > 0)
{
return false;
}
else
{
return true;
}
}
//// THE TEXT (source) file //////////////////////////////////
namespace ANamespace
{
using System;
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine("Hello World");
}
}
}
////// END Of TEXT /////////////////////////////////
No matter what i do i continuously get this error. I can put the 'using
System;' above the namespace. I can even take the entire namespace context
out and i still get this error. I also checked out the error online and the
code snippet i'm using simply doesn't seem to be breaking any rules?!
Any help ASAP, very appreciated.
Would even be obliged for some very exacting tutorials on everything about
Mono CodeDom and Compiler, if they exist.
Also, i'm using Mono on Windows. Does the CSharp and CodeDom stuff simply
not work properly on Windows?
Thanks, much.
A Newb
--
View this message in context: http://mono.1490590.n4.nabble.com/CSharpCodProvider-problems-tp2068477p2068477.html
Sent from the Mono - Dev mailing list archive at Nabble.com.
More information about the Mono-devel-list
mailing list