天天看點

C# 反射的一些總結

從配置檔案讀取這個方法所在的命名空間,類名,方法名。

執行方法,擷取傳回值。

//拼出dll所在的路徑

string path = Assembly.GetExecutingAssembly().Location;

path = path.Substring(0, path.LastIndexOf("//")) + "//Lib//";

path += ConfigManager.GetValue("BLLDisposalFileName");

//加載這個dll

Assembly assembly = Assembly.LoadFrom(path);

//命名空間

string classPath = ConfigManager.GetValue("BLLDisposalNameSpace");

Type type = (Type)assembly.GetType(classPath);

if (type == null)

{

log.log("type建立錯誤");

}

//擷取類

object classObj = Activator.CreateInstance(type);

//傳參數

object[] objParams = { (object)resultMsgBody };

if (classObj == null)

{ log.log("類: " + null + "參數:" + objParams.ToString()); }

else

{

log.log("類: " + classObj.ToString() + "參數:" + objParams.ToString());

}

//執行類,傳回傳回值

object obj = type.InvokeMember("GetDisposalBLL", BindingFlags.Default | BindingFlags.InvokeMethod, null, classObj, objParams);

if (obj == null)

log.log("調用失敗");

log.log("調用成功" + obj.ToString());

//擷取傳回值

resultMsgBody = obj.ToString();