C# Mircosoft.CSharp.Compiler?

Status
Not open for further replies.
Can anyone help me get the Compile() method working. Here is what I have;

Code:
string[] files = {'"' + "C:\\Documents and Settings\\Dom\\My Documents\\My Coding\\C#\\Applications\\Coder\\CoderApp.cs" + '"'};
   string[] name = {"CoderApp.cs"};
   string[] refs = {"Cscompmgd.dll"};
   System.Console.WriteLine(Microsoft.CSharp.Compiler.Compile(files, name, "CoderApp2.exe", refs, null)[0].ToString());
   }

This prints the error;

C:\Documents and Settings\Dom\My Documents\My Coding\C#\Applications\Coder\Coder
App.cs(1,1): error CS0116: A namespace does not directly contain members such as fields or methods

I can't figure out the second argument for the compile method (Prefer the java api documentation) what do I need here?



Parameters
sourceTexts
An array of type string, each with C# code. Each string is the equivalent of a single source file. There must be at least one sourceText element.
sourceTextNames
An array of type string containing the filenames for each sourceText. The filenames are used for compiler diagnostics and debug information. This array is required, but can be locally overridden by use of the #line directive. This array must have the same number of elements as the sourceTexts parameter.
target
The output filename. This argument is required.
imports
An array of type string, with files to reference. This is the same as the /reference (Import Metadata) compiler option. This parameter can be a null reference (Nothing in Visual Basic).
options
A IDictionary of compiler options. The key is the short form of the compiler option. The value is the value desired for the option. This parameter can be a null reference (Nothing in Visual Basic). Note that not all of the C# Compiler Options are recognized. Recognized options and values:


Thanks again
 

Horatio

Ars Legatus Legionis
24,431
Moderator
I've gotten it working, but I don't have my code available :( The 'sourceTexts' parameter is a string[] that contains the actual program texts, not a file name. The 'sourceTextNames' parameter is what you'd be calling those files. Also, you need to set up some appropriate switches.
Code:
using System;
using Microsoft.CSharp;
using System.IO;
using System.Collections;

namespace CompilerTest
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[MTAThread]
		static void Main(string[] args)
		{
			//
			// TODO: Add code to start application here
			//
			int n = 0;
			string[] CompileCode = new string[args.GetLength(0)];
			Hashtable ht = new Hashtable();
			ht.Add("target", "exe");


			foreach(string i in args)
			{
				TextReader tr = new StreamReader(i);
				CompileCode[n++] = tr.ReadToEnd();
				Console.WriteLine("Reading file " + i);
			}
			CompilerError[] CompilerErrors = Compiler.Compile(CompileCode,args, "test.exe",null,ht);
			foreach(CompilerError i in CompilerErrors)
				Console.WriteLine(i.ToString());
		}
	}
}

Edit: Posted the working code.

[This message was edited by Horatio on April 15, 2002 at 14:06.]
 
Status
Not open for further replies.