Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to compile C# in C#
#1
Information 
Requirements:
- Microsoft Visual C# Express / Visual Studio Beta 11.
- .Net Framework (to the latest version will be the best)
- Basic Understanding of C#
- Your Brain

Objective:
To be able to compile a C# code by using CodeDom. The outcome of this is a form application.

1. Create a new Form
[spoiler][Image: zjyrs2.jpg][/spoiler]


2. Add in some GUI
An optimum requirement of:
2 TextBoxes (textBox1, textBox2)
1 Button
[spoiler][Image: 34q1h1h.jpg][/spoiler]

3. Include some namespaces
Quote:using System;
using System.Windows.Forms;
using System.CodeDom.Compiler;
[spoiler][Image: r0e6mh.jpg][/spoiler]


4. Make the variables in the button function
Quote:string SourceCode = textBox1.Text;
string Output = "Output.exe";
It's OK if you have a warning because we haven't used Output variable yet.
[spoiler][Image: zmd3lx.jpg][/spoiler]

5. Make the parameters
These settings are to compile forms.

Quote:String[] Assembly = { "System.dll","System.Drawing.dll","System.Windows.Forms.dll" };
CodeDomProvider CodeCompiler = CodeDomProvider.CreateProvider("CSharp");
CompilerParameters Parameters = new CompilerParameters(Assembly, "");

Parameters.OutputAssembly = Output;
Parameters.GenerateExecutable = true;
Parameters.GenerateInMemory = false;
Parameters.WarningLevel = 3;
Parameters.TreatWarningsAsErrors = true;
Parameters.CompilerOptions = "/optimize /target:winexe";

[spoiler][Image: vpz4o7.jpg][/spoiler]

6. Debugging and Compiling
This is the part where it compiles the input code.
If the input code fails we need to put the errors together as well.

Quote:string Errors = null;
try
{
CompilerResults Results = null;
Results = CodeCompiler.CompileAssemblyFromSource(Parameters, SourceCode);
if (Results.Errors.Count > 0)
{
Errors = "";
foreach (System.CodeDom.Compiler.CompilerError CompileError in Results.Errors)
{
Errors += "Line number " + CompileError.Line + ", Error Number: " + CompileError.ErrorNumber + ", '" + CompileError.ErrorText + ";\r\n\r\n";
}
}
}
catch (Exception Exceptions)
{
Errors = Exceptions.Message;
}

[spoiler][Image: 2qvghfm.jpg][/spoiler]


7. Displaying the outcome
Displaying on the debugging textBox either compiled or failed.
Quote:if (Errors == null)
{
textBox2.Text = Output + " Compiled!";
}
else
{
textBox2.Text = "Error occurred during compilation : \r\n" + Errors;
}

[spoiler][Image: 10h24w9.jpg][/spoiler]

8. Build the program and insert this template.
Quote:using System.Windows.Forms;
using System;
namespace TestProgram
{
public static class Program
{
public static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyForm());
}
}
public class MyForm:Form
{
public MyForm()
{
MessageBox.Show("Tutorial by El Spiko");
}
}
}
[spoiler][Image: sfzas3.jpg][/spoiler]

As I said it was to compile forms.
You can always remove the full namespaces. I have left them because I expect some people to not fully understand the namespaces.

This is an old tut I posted awhile back on HF enjoy.

- Guntile
Reply
#2
Nice tut Brian Big Grin, It seems hard lol.
Reply
#3
(08-08-2012, 02:31 PM)Apex Wrote: Nice tut Brian Big Grin, It seems hard lol.

Haha, Yeah It's a little basic/complex. Depending on your level, Anyways glad you joined the forum!
Reply
#4
Nice tutorial! Smile Well written! Tongue
Reply
#5
(08-08-2012, 02:49 PM)cawex Wrote: Nice tutorial! Smile Well written! Tongue

Thanks man I really appreciate the support! I'm looking forward to making more.
Reply
#6
(08-08-2012, 06:02 PM)Guntile Wrote:
(08-08-2012, 02:49 PM)cawex Wrote: Nice tutorial! Smile Well written! Tongue

Thanks man I really appreciate the support! I'm looking forward to making more.

Thanks for your plans :biggrin: Creating tutorials is helpful to the community and can rarely bring you prizes like credits Angel
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)