希望有人能幫助我。基本上我有動態加載組件2例如加載程序集調用方法返回自定義類型
ObjA= Assembly.LoadFrom(folder + module); //had the main methods of the integration
ObjB = Assembly.LoadFrom(folder + module_decl); //has more interface declarations for the intigration
現在ObjB
具有用於在objA
的方法返回的值聲明。因此,我第一次嘗試,找到這個返回值,並創建它的類型,例如
Type intObj ;
Type[] type2 = objB.GetTypes();
foreach (Type t in type2)
{
if ((intObj == null) && (t.FullName == "IIReturnInterfaceDecl"))
{
intObj = t;
}
}
然後我去,創建主類的實例,並調用方法
foreach (Type t in types)
{
mi = t.GetMethod("CreateTheObjectMethod");
if (mi != null)
{
string typeName = t.FullName;
object lateBoundObj = null;
lateBoundObj = Activator.CreateInstance(t);
intObj = lateBoundObj.GetType().InvokeMember("CreateTheObjectMethod",
//BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance,
BindingFlags.InvokeMethod | BindingFlags.GetProperty,
null,
lateBoundObj,
args);
if (intObj == null)
{
MessageBox.Show("oops");
}
else
{
MessageBox.Show("done");
}
//
// break;
}
//result
}
}
else
{
MessageBox.Show("DAMMIT");
}
但基本上我不能得到它來調用方法和返回對象的方式我想,(上面的代碼無法編譯給人錯誤
錯誤1無法隱式轉換類型「對象」到「的System.Type」。一 顯式轉換存在(是否缺少強制轉換?)「)
但我不知道如何得到它來將它轉換爲正確的類型。
請注意,我加載的外部程序集是第三方,我沒有頭文件或聲明他們我可以訪問他們,但我沒有界面或類聲明爲我輕鬆投下,我必須做到這一點動態。
真的很感激任何指導。我查了很多,並在'委託'上看到了很多,但我看到的最多的是進行調用並傳遞值,而不是處理要返回並使用它們的動態用戶類型值。
感謝您的任何幫助。
//編輯:當初錯誤的if語句,比較遺憾的是對於那些已經作出迴應,一定是:如果(intObj == NULL),而不是如果(CreateTheObjectMethod == NULL)
這可能有助於,這基本上是我打電話的方法的聲明
[System.Reflection.RuntimeMethodInfo] = {IIReturnInterfaceDecl CreateTheObjectMethod(System.String, System.String)}
什麼行給出這個錯誤? – 2013-04-10 10:43:35
在創建類型的實例之後,爲什麼您要再次獲取類型?與您要運行的方法相同。 – Dilshod 2013-04-10 10:45:52
@Daniel Hilgarth - 我得到錯誤,我在做的調用「intObj = lateBoundObj.GetType()。InvokeMember( 「CreateTheObjectMethod」, //BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance, BindingFlags.InvokeMethod | BindingFlags.GetProperty, 空, lateBoundObj, 參數);」 – Johannes 2013-04-10 10:48:22