進入 .NET Framework 目錄(如 `C:\Windows\Microsoft.NET\Framework\v4.0.30319`),使用 `csc.exe`:
csc /target:library /out:MyLib.dll MyCode.cs
csc /t:library /out:MyLib.dll MyCode.cs MyCode2.cs // 多檔合併
csc /target:library /debug /optimize- /out:MyLib.dll *.cs // 偵錯版
csc /target:library /warn:0 /nologo /out:MyLib.dll *.cs // 無警告、無 Logo
// MathUtils.cs
namespace MyUtils {
public static class MathHelper {
public static long GCD(long a, long b) {
while (b != 0) { long t = a % b; a = b; b = t; }
return a;
}
public static void Swap<T>(ref T a, ref T b) {
T tmp = a; a = b; b = tmp;
}
}
}
csc /target:library /out:MyUtils.dll MathUtils.cs
using MyUtils;
long gcd = MathHelper.GCD(48, 18); // 6
int x = 10, y = 20;
MathHelper.Swap(ref x, ref y); // x=20, y=10
Assembly asm = Assembly.LoadFrom(@"C:\Libs\MyUtils.dll");
Type type = asm.GetType("MyUtils.MathHelper");
MethodInfo mi = type.GetMethod("GCD");
object result = mi.Invoke(null, new object[] { 48L, 18L }); // 6
aspnet_compiler -v /MySite -p C:\Source\MySite C:\Deploy\MySite_Precompiled