C# - Writing your own .NET Compiler
This article provides a simple example of how to compile your .NET code to a DLL using a batch/command file to call CSC.exe.
Follow these simple steps and you will have a compiled .NET dll in a few minutes:
Step 1: Create a temporary folder, for this example I will use C:\temp\compile
Step 2: Create a class file, C:\temp\compile\TestClass.cs, and paste the following code into it and save.
namespace CompileDotNet
{
public class TestClass
{
public string ClassName
{
get { return "TestClass"; }
}
}
}
Step 3: Create an AssemblyInfo class file, C:\temp\compile\AssemblyInfo.cs, and paste the following code into it and save.
using System.Reflection;
using System.Runtime.CompilerServices;
[assembly: AssemblyTitle("CompileDotNet")]
[assembly: AssemblyDescription("CompileDotNet v 1.0")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("CompileDotNet")]
[assembly: AssemblyProduct("CompileDotNet v 1.0")]
[assembly: AssemblyCopyright("Copyright 2007.")]
[assembly: AssemblyTrademark("CompileDotNet")]
//[assembly: AssemblyCulture("en")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.0.0.0")]
//[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyDelaySign(false)]
Step 4: Create a batch/make file, C:\temp\compile\build.cmd, and paste the following code into it and save.
@echo off
REM *** csc parameters ***
SET dll=CompileDotNet.dll
SET src=%cd%
SET dst=bin
SET csc=C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe /nologo
SET out=/out:%src%\%dst%\%dll%
SET target=/target:library
SET recurse=/recurse:%src%\*.cs
REM *** Referrences ***
SET ref=
REM *** delete existing dlls, etc ***
del /F /Q %src%\%dst%\*.*
REM *** compile the dll ***
%csc% %out% %target% %ref% %recurse%
Step 5: Create a bin folder, C:\temp\compile\bin\.
Step 6: Open a command prompt window and go to the C:\temp\compile\ folder ( type "cd\temp\compile" and press Enter).
Step 7: Build the dll, in the command prompt window type "build" and press Enter.
Step 8: In Windows Explorer, go to the C:\temp\compile\bin\ folder and right click on the CompileDotNet.dll file and click the Properties menu to see the Product Name and Version information that was specified in the AssemblyInfo.cs class.
Oila!
Wednesday, March 10, 2010 7:50:42 PM