使用Assembly生成带参数的泛型实例
public class DataAccess<T>
{
protected static readonly string _path = ConfigurationSettings.AppSettings["WebDAL"];
public static IDAL.ITreeTable<T> CreatTreeTable(string tableName, string idName, string parentIdName, string displayOrderName)
{
if (string.IsNullOrEmpty(tableName) || string.IsNullOrEmpty(idName) || string.IsNullOrEmpty(parentIdName))
throw new Exception("tableName,idName,parentIdName,displayOrder必须设置.");
string className = _path + ".TreeTable`1[[" + typeof(T).FullName + "," + typeof(T).Assembly.ToString() + "]]";
Assembly assembly = Assembly.Load(_path);
Type t = assembly.GetType(className);
object[] args = new object[4];
args[0] = tableName;
args[1] = idName;
args[2] = parentIdName;
args[3] = displayOrderName;
return (IDAL.ITreeTable<T>)Activator.CreateInstance(t, args);
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步