我最近創建了一個界面層來區分DataAccessProvider和我們的業務邏輯層。 通過這種方法,我們可以通過更改Web/App.Config中的值來隨時更改我們對DataAccessProvider的選擇。 (如果需要,可以提供更多細節)。反射速度有多慢
無論如何,要做到這一點,我們使用反射來完成我們可以工作的DataProvider類。
/// <summary>
/// The constructor will create a new provider with the use of reflection.
/// If the assembly could not be loaded an AssemblyNotFoundException will be thrown.
/// </summary>
public DataAccessProviderFactory()
{
string providerName = ConfigurationManager.AppSettings["DataProvider"];
string providerFactoryName = ConfigurationManager.AppSettings["DataProviderFactory"];
try
{
activeProvider = Assembly.Load(providerName);
activeDataProviderFactory = (IDataProviderFactory)activeProvider.CreateInstance(providerFactoryName);
}
catch
{
throw new AssemblyNotFoundException();
}
}
但是現在我想知道反射速度有多慢?
當然,創建一個測試工具來測試它是很簡單的嗎? – marijne 2009-04-21 08:05:36
如果工廠是單身人士,那麼Assembly.Load只會被調用一次? – CVertex 2009-04-21 08:07:40
http://stackoverflow.com/questions/25458/how-costly-is-net-reflection?rq=1 – nawfal 2013-06-08 23:50:30