有沒有辦法在不同類型的不同組件之間進行強制轉換?我需要執行一個函數,它的程序集已經加載了Assembly.Load(ReadAllBytes(...)),但是它在參數投射中失敗了。那麼,有沒有什麼辦法可以在c#中「reinterpret_cast」對象?強制鑄造一種組裝類型到另一種組裝類型在c#
編輯我鑄鋼問題的
最基本的例子是:
Assembly ass = Assembly.Load(File.ReadAllBytes("external.dll"))
object other_type_instance = ass.GetType("OtherType").InvokeMember(null, BindingFlags.CreateInstance, null, null, new Object[]{});
OtherType casted_isntance = (OtherType)other_type_instance; // fails with runtime error, because there are two OtherType:s classes loaded.
請提供一些代碼 – 2010-05-11 21:12:35
我認爲你會更樂意編寫一個適配器類來包裝一種類型並提供與另一種類型兼容的接口。當然,如果其他類型是密封的或其他不可變的,那麼你很大程度上沒有運氣。 – 2010-12-10 19:49:19