2013-04-10 82 views
1

希望有人能幫助我。基本上我有動態加載組件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)} 
+0

什麼行給出這個錯誤? – 2013-04-10 10:43:35

+0

在創建類型的實例之後,爲什麼您要再次獲取類型?與您要運行的方法相同。 – Dilshod 2013-04-10 10:45:52

+0

@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

回答

0

你試過類似的東西嗎?

   mi = t.GetMethod("CreateTheObjectMethod"); 

       if (mi != null) 
       { 
        string typeName = t.FullName; 
        object lateBoundObj = null; 
        lateBoundObj = Activator.CreateInstance(t); 

        object returnValue = mi.Invoke(lateBoundObj,null);//pass your params instead of null if you need 
        //here you can check what return value is. 
        Type returnedType = returnValue.GetType(); 


        if (CreateTheObjectMethod== null) 
        { 
         MessageBox.Show("oops"); 
        } 
        else 
        { 
         MessageBox.Show("done"); 
        } 
        // 
        // break; 

       } 
+0

沒有不是真的,但它會自動創建該方法的對象嗎?我會嘗試但不知道這部分「if (CreateTheObj ectMethod == null)「 – Johannes 2013-04-10 11:13:42

+0

@Johannes我不確定你到底想要什麼。 「創建方法的對象」是什麼意思? – Dilshod 2013-04-10 11:24:39

+0

對不起,我現在意識到,我在if語句中粘貼了錯誤的文本,它肯定是if(intObj == null),而不是if(CreateTheObjectMethod == null),對此抱歉,會看看是否有編輯原始代碼的方法,但基本上我想調用方法「CreatObjectOfMethod」,我使用它的返回值。但是我不能轉換成它的返回值類型,因爲類型是我在編譯時沒有的usertype(class),只有運行時。而如果我只做類型對象的返回值,那麼我只能得到整個時間的空值。 – Johannes 2013-04-10 11:47:10

0

intObjSystem.Type類型和您要的InvokeMember的返回值分配給它。但返回值是object。這就是錯誤信息告訴你的。

您可以簡單地將返回值轉換爲Type,但我懷疑這是否正確。這將擺脫編譯器錯誤,但如果被調用的方法(CreateTheObjectMethod)的返回值不是Type類型,則會在運行時導致異常。
您需要將返回值分配給返回類型爲CreateTheObjectMethod的變量,並相應地返回InvokeMember

+0

這是我在開始時嘗試創建「intObj = t;」時所嘗試的內容我這樣做的接口類型(t.FullName ==「IIReturnInterfaceDecl」)。但後來我錯了,不知道如何強制Method調用返回該類型(轉換對象的返回值)。 – Johannes 2013-04-10 11:08:29

+0

@Johannes:你在混淆Type類型的實例和類型爲'IIReturnInterfaceDecl'的實例。在這種情況下,'intObj'只需要是'object'類型,因爲我認爲在編譯時'IIReturnInterfaceDecl'類型是未知的。 – 2013-04-10 11:12:06

+0

這是我的想法,但如果我正常調用它並使對象類型爲intObj,那麼它總是返回null。這就是爲什麼我開始嘗試這個。但是如果我在設計時將組件集合放入IDE,那麼我可以毫無錯誤地完成這些調用,但是不會動態地執行這些調用。不幸的是,我需要他們是動態的。 – Johannes 2013-04-10 11:17:04