標籤:net load 擷取 nbsp rom pil except name catch
以下類中有三個方法:
LoadAssembly:載入指定路徑的程式集
GetInstance:根據Type動態擷取執行個體,用泛型接到返回的類型
ExecuteMothod:執行執行個體中的指定方法
/// <summary> /// 繼承自MarshalByRefObject表示允許跨域通訊 /// </summary> public class RemoteLoader : MarshalByRefObject { private Assembly _assembly; public void LoadAssembly(string assemblyFile) { try { _assembly = Assembly.LoadFrom(assemblyFile); //return _assembly; } catch (Exception ex) { throw ex; } } public T GetInstance<T>(string typeName) where T : class { if (_assembly == null) return null; var type = _assembly.GetType(typeName); if (type == null) return null; return Activator.CreateInstance(type) as T; } public void ExecuteMothod(string typeName, string methodName) { if (_assembly == null) return; var type = _assembly.GetType(typeName); var obj = Activator.CreateInstance(type); Expression<Action> lambda = Expression.Lambda<Action>(Expression.Call(Expression.Constant(obj), type.GetMethod(methodName)), null); lambda.Compile()(); } }
asp.net動態載入程式集建立指定類的執行個體及調用指定方法