ASP.NET 動態編譯的 App_Code 目錄下類別不在主組件中,需透過特定組件名稱載入。
// ASPx.cs
using System;
using System.Reflection;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Assembly asm = Assembly.Load("__code"); // 或 "app_code"
Type tp = asm.GetType("Tools");
if (tp != null)
{
object o = Activator.CreateInstance(tp); // 建立實例
MethodInfo mi = tp.GetMethod("Reg"); // 取得方法
object ret = mi.Invoke(o, null); // 執行方法
string str = ret.ToString(); // 取得結果
}
}
}
using System;
public class Tools
{
public string Reg()
{
return "aa";
}
}