我有一個ASP.net應用程序引用一個外部程序集,我需要動態加載並發現實現已知接口的任何類型。我遇到的問題是,我反映的類型與正在運行的接口不匹配,因此我無法投射它。在ASP.net環境下反映的問題
實施例:
該代碼在ASP.net應用運行。
var assembly = Assembly.LoadFile(Path.Combine(HttpRuntime.BinDirectory, "ExternalAssembly.dll"));
var type = assembly.GetExportedTypes().First<Type>(x => x.Name == "AClass"); // AClass implements IAInterface
var reflectedInterface = type.GetInterface(typeof(IAmAInterface).ToString());
if (reflectedInterface != typeof(IAmAInterface))
throw new Exception("This makes me sad"); // This code gets run
我可以我從料箱和所述接口從typeof運算返回加載的反射界面之間看到的唯一區別是,將typeof組件具有在臨時ASP.net路徑(C的位置:\ Windows \微軟.NET \ Framework \ v2.0.50727 \ Temporary ASP.NET Files \ root \ 08c43c8b \ 3adac8cf \ assembly \ dl3 \ eb7a4127 \ 0235ea60_a3c8c901 \ ReflectionTest.DLL)
感謝Paul Alexander我更改了使用Assembly的代碼。 Load方法不是Assembly.LoadFile,它解決了這個問題。
wwilden:我也嘗試提取接口到它自己組裝,這是否也是解決問題。