CSharp Compiler / Interpretor

  • Thread starter Thread starter Azeem M. Suleman
  • Start date Start date
A

Azeem M. Suleman

Hello,

I need to use Csharp compiler and an interpretor. In my application user
will use the application and application will write CSharp code on backend.
But now i don't need to reinvent the wheel, just need to know how i can give
user an option to build the project which will compile and make an
executable file.

thanks.
 
Azeem said:
Hello,

I need to use Csharp compiler and an interpretor. In my application
user will use the application and application will write CSharp code
on backend. But now i don't need to reinvent the wheel, just need to
know how i can give user an option to build the project which will
compile and make an executable file.

thanks.

Check out the Microsoft.CSharp.Compiler class.

- Pete
 
Yeah i looked on msdn, is their any implementation sample for it. Like for
basic idea i found this article:

http://support.microsoft.com/defaul...port/kb/articles/Q304/6/55.asp&NoWebContent=1

But it didn't compile windows form and other controls.

Thanks.
Miha Markic said:
Hi Azeem,

Check out Microsoft.CSharp.Compiler class.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com
Azeem M. Suleman said:
I need to use Csharp compiler and an interpretor. In my application user
will use the application and application will write CSharp code on backend.
But now i don't need to reinvent the wheel, just need to know how i can give
user an option to build the project which will compile and make an
executable file.

thanks.
 
Hi,

You'll have to specify parameters.ReferencedAssemblies to let compiler knows
which assemblies you reference in your code.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Azeem M. Suleman said:
Yeah i looked on msdn, is their any implementation sample for it. Like for
basic idea i found this article:

http://support.microsoft.com/defaul...port/kb/articles/Q304/6/55.asp&NoWebContent=1

But it didn't compile windows form and other controls.

Thanks.
Miha Markic said:
Hi Azeem,

Check out Microsoft.CSharp.Compiler class.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com
Azeem M. Suleman said:
I need to use Csharp compiler and an interpretor. In my application user
will use the application and application will write CSharp code on backend.
But now i don't need to reinvent the wheel, just need to know how i
can
give
user an option to build the project which will compile and make an
executable file.

thanks.
 
Where to reference that dll, as i'm just using this code. Any sample...Here
is the concept code that i saw from the url:

private void button1_Click(object sender, System.EventArgs e)
{
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
ICodeCompiler icc = codeProvider.CreateCompiler();
string Output = "Output.exe";
Button ButtonObject = (Button) sender;

textBox2.Text = "";
System.CodeDom.Compiler.CompilerParameters parameters = new
CompilerParameters();
//Make sure we generate an EXE, not a DLL
parameters.GenerateExecutable = true;
parameters.OutputAssembly = Output;
CompilerResults results =
icc.CompileAssemblyFromSource(parameters,textBox1.Text);

if (results.Errors.Count > 0)
{
textBox2.ForeColor = Color.Red;
foreach(CompilerError CompErr in results.Errors)
{
textBox2.Text = textBox2.Text +
"Line number " + CompErr.Line +
", Error Number: " + CompErr.ErrorNumber +
", '" + CompErr.ErrorText + ";" +
Environment.NewLine + Environment.NewLine;
}
}
else
{
//Successful Compile
textBox2.ForeColor = Color.Blue;
textBox2.Text = "Success!";
//If we clicked run then launch our EXE
if (ButtonObject.Text == "Run") Process.Start(Output);
}

AirPete said:
Azeem said:
Yeah i looked on msdn, is their any implementation sample for it.
Like for basic idea i found this article:
http://support.microsoft.com/defaul...port/kb/articles/Q304/6/55.asp&NoWebContent=1
But it didn't compile windows form and other controls.

Did you reference System.Windows.Forms.dll?
[snip]
 
I found the solution.

I just added:

parameters.ReferencedAssemblies.Add("System.dll");

parameters.ReferencedAssemblies.Add("System.Drawing.dll");

parameters.ReferencedAssemblies.Add"System.Windows.Forms.dll");

It works fine. But it always loads from command prompt. How to handle that.
I just need to pass C# code to compiler and generate executable in such a
way that no process can be seen.

Thanks.
 
Azeem said:
I found the solution.

I just added:

parameters.ReferencedAssemblies.Add("System.dll");

parameters.ReferencedAssemblies.Add("System.Drawing.dll");

parameters.ReferencedAssemblies.Add"System.Windows.Forms.dll");

It works fine. But it always loads from command prompt. How to handle
that. I just need to pass C# code to compiler and generate executable
in such a way that no process can be seen.

parameters.CompilerOptions = "/target:winexe";

Not tested, but should work.

- Pete
[snip]
 
What to do for multiple files. Like if i have 3 class files at different
places and they all are using objects of each others....what will be the
parameter.....

Thanks.

AirPete said:
Azeem said:
I found the solution.

I just added:

parameters.ReferencedAssemblies.Add("System.dll");

parameters.ReferencedAssemblies.Add("System.Drawing.dll");

parameters.ReferencedAssemblies.Add"System.Windows.Forms.dll");

It works fine. But it always loads from command prompt. How to handle
that. I just need to pass C# code to compiler and generate executable
in such a way that no process can be seen.

parameters.CompilerOptions = "/target:winexe";

Not tested, but should work.

- Pete
[snip]
 
Back
Top