2
我試圖從僅在運行時已知的程序集中創建類。有奇怪的問題。在鑄造通過反射創建的類時遇到問題
string providerType = AppConfig.GetConfigValue("LocationProvider");
string assemblyFileName = AppConfig.GetConfigValue("LocationProviderAssembly");
Assembly assembly = Assembly.LoadFrom(assemblyFileName);
//Object obj = assembly.CreateInstance(providerType) ;
Type type = assembly.GetType(providerType);
Object obj = Activator.CreateInstance(type);
ILocationProvider locProvider = obj as ILocationProvider;
float distance = locProvider.GetDistance(new Location(), new Location());
如果我使用調試器,並停止最後兩行被執行之前,我可以運行在即時窗口,這兩條線,和他們的工作。但是如果我讓代碼運行,locProvider仍然爲空。
OBJ似乎有正確的類型:
obj.GetType()
{Name = "LocationProviderConcrete" FullName = "LocationProvider.LocationProviderConcrete"}
[System.RuntimeType]: {Name = "LocationProviderConcrete" FullName = "LocationProvider.LocationProviderConcrete"}
base {System.Reflection.MemberInfo}: {Name = "LocationProviderConcrete" FullName = "LocationProvider.LocationProviderConcrete"}
這工作正常立即窗口:
((ILocationProvider) obj).GetDistance(new Location(), new Location())
任何線索,爲什麼這不正常碼流工作?
就是這樣。謝謝,喬恩! – 2012-02-24 06:38:39