Total members 11894 |It is currently Sat Nov 23, 2024 12:40 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka






 Project Name:   Mini C# Compiler
 Programmer:   ms_soft89
 Type:   Compiler
 Technology:  C#
 IDE:   NONE
 Description:   C# MiniCompiler is a simple application that uses CSharpCodeProvider to create an instance of C# Code Compiler, then uses ICodeCompiler for all .NET Compilers like VB, J#. So if we want to Compile C# Code to assign the interface ICodeProvider to provider.CreateCompiler().
Attachment:
MiniCom.png
MiniCom.png [ 56.37 KiB | Viewed 6465 times ]

Code sample :
csharp code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
using System.IO;
using System.Diagnostics;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
namespace MiniCompiler
{

public class ErrorEventArgs : EventArgs
{

public readonly List<string> Errors = new List<string>();
public ErrorEventArgs(List<string> errors)
{
this.Errors = errors;
}
}
public delegate void ErrorEventHandler(Object sender , ErrorEventArgs e);


class CompilationClass
{
[DllImport("Kernel32.dll")]
private static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll")]
private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);


public CompilationClass()
{
RefAssemblies = new List<Assembly>();
}
List<Assembly> assemblies = new List<Assembly>();
public List<Assembly> RefAssemblies { get { return assemblies; } set { assemblies = value; } }


private static IEnumerable<string> GetFilesRecursive(string dirPath)
{
DirectoryInfo dinfo = new DirectoryInfo(dirPath);
return GetFilesRecursive(dinfo, "*.dll*");
}

private static IEnumerable<string> GetFilesRecursive(DirectoryInfo dirInfo, string searchPattern)
{
foreach (DirectoryInfo di in dirInfo.GetDirectories())
foreach (string fi in GetFilesRecursive(di, searchPattern))
yield return fi;

foreach (FileInfo fi in dirInfo.GetFiles(searchPattern))
yield return fi.FullName;
}

public List<string> AssemblyDirectories()
{
List<string> assemblies = new List<string>();
Microsoft.Win32.RegistryKey key = Registry.LocalMachine;
key = key.OpenSubKey(@"SOFTWARE\Microsoft\.NETFramework\AssemblyFolders");
string[] SubKeynames = key.GetSubKeyNames();
foreach (string subkeys in SubKeynames)
{
RegistryKey subkey = Registry.LocalMachine;
subkey = subkey.OpenSubKey(@"SOFTWARE\Microsoft\.NETFramework\AssemblyFolders\" + subkeys);
string[] l = subkey.GetValueNames();
foreach (string v in l)
{
if (subkey.GetValueKind(v) == RegistryValueKind.String)
{
string value = (string)subkey.GetValue(v);
assemblies.Add(value);
}
}
}
assemblies.Add( Environment.SystemDirectory+ @"..\..\Windows\Microsoft.NET\Framework\v2.0.50727");
return assemblies;
}
public List<string> GetAssembliesFile( )
{
List<string> refAssemblies = new List<string>();


if (!File.Exists(Environment.CurrentDirectory+ "\\assemblies.sft"))
{


List<string> dirs = AssemblyDirectories();

foreach (string dir in dirs)
{


foreach (string assemblyfile in GetFilesRecursive(dir))
{


refAssemblies.Add(assemblyfile);

}
}
foreach (string assemblyfile in System.IO.Directory.GetFiles(@"C:\Windows\Microsoft.NET\Framework"))
{
FileInfo fino = new FileInfo(assemblyfile);
refAssemblies.Add(fino.FullName);

}
SerializeAssemblyDirs(refAssemblies);
}
else
{
BinaryFormatter binary = new BinaryFormatter();
Stream sreader = new FileStream(Environment.CurrentDirectory + "\\assemblies.sft", FileMode.Open);

refAssemblies = (List<string>)binary.Deserialize(sreader);
sreader.Close();
}
return refAssemblies;

}
public void SerializeAssemblyDirs(List<string> assemblies )
{

Stream str = new FileStream(Environment.CurrentDirectory + "\\assemblies.sft", FileMode.OpenOrCreate);
BinaryFormatter binary = new BinaryFormatter();
binary.Serialize(str, assemblies);
}

public event ErrorEventHandler CompilationError;
protected virtual void OnCompilationError(ErrorEventArgs e)
{
if (CompilationError != null)
{
CompilationError(this, e);
}
}
public event EventHandler FileNotExists;
protected virtual void OnFileNotExists(EventArgs e)
{
if (FileNotExists != null)
{
FileNotExists(this, e);
}
}
public static string Classname(string code)
{
string mainNameSpace = string.Empty;
List<string> classcode = new List<string>();
List<string> classname = new List<string>();
List<string> NamespaceCode = new List<string>();
List<string> nmSpaceName;
string mainClass = string.Empty;
ManiPulateMainClass.ManipulateNameSpace(code, out NamespaceCode, out nmSpaceName);
for (int x = 0; x < nmSpaceName.Count; x++)
{
ManiPulateMainClass.ManipulateClasses(NamespaceCode[x], out classname, out classcode);


mainClass = ManiPulateMainClass.GetMainClass(classname, classcode);

mainNameSpace = nmSpaceName[x];



}
string[] mainClassName = mainClass.Split(':');
return string.Format("{0}.{1}", mainNameSpace.Trim(), mainClassName[0].Trim());
}




public void CompileCSharp(string code,string outputassembly )
{

CSharpCodeProvider provider = new CSharpCodeProvider();
ICodeCompiler compiler = provider.CreateCompiler();



#region CompilerParameters
CompilerParameters parameters = new CompilerParameters()
{
MainClass = Classname(code),
GenerateExecutable = true,
OutputAssembly = outputassembly,
IncludeDebugInformation = true,

};


parameters.ReferencedAssemblies.Clear();
foreach (Assembly asm in assemblies)
{

parameters.ReferencedAssemblies.Add(asm.Location);
}



#endregion
CompilerResults result = compiler.CompileAssemblyFromSource(parameters, code);
if (result.Errors.Count == 0)
{
if (!System.IO.File.Exists(outputassembly))
{
OnFileNotExists(new EventArgs());
return;
}
ProcessStartInfo startInfo = new ProcessStartInfo();

startInfo.FileName =outputassembly;

startInfo.WindowStyle = ProcessWindowStyle.Maximized;
startInfo.Arguments = "\\S";
Process process = new Process();

process.StartInfo = startInfo;

process.Start();
IntPtr hwnd = GetConsoleWindow();
ShowWindow(hwnd, 0);

}
else
{

foreach (CompilerError er in result.Errors)
{


List<string> cerrors = new List<string>();
cerrors.Clear();
cerrors.Add(er.ErrorText+ " Line ("+er.Line.ToString()+") error :"+er.ErrorNumber );
OnCompilationError(new ErrorEventArgs(cerrors));

}
result.Errors.Clear();
}


}
}
}





Attachments:
MiniCompiler.zip [217.36 KiB]
Downloaded 2040 times

_________________
Please recommend my post if you found it helpful. ,
java,j2ee,ccna ,ccnp certified .
Author:
Expert
User avatar Posts: 838
Have thanks: 2 time

COOL



Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time

updated.


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 3 posts ] 

  Related Posts  to : Mini C# Compiler
 compiler code of syntax analyzer in C++     -  
 code for my mini project     -  
 I am doing my BCA final year. Looking out for a mini project     -  
 need mini chat voice project.     -  
 Java Web Browser (Mini Project)     -  
 prapose me a web based mini project     -  
 PC health of remote sever (Mini Project)     -  



Topic Tags

C# Projects
cron






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com