我實際上正在做這樣的事情。大會被探查,如果他們存在/將工作。
您可以在許多方面,當然,做到這一點,但是這就是我結束了:
- 提取這個類到接口的所有功能(我們稱之爲:IExcel),並加入IsAvailable屬性,它
- 實現了實現IExcel的僞類,並從IsAvailable返回'false'(當然還有從其他方法拋出NotSupportedException)。
- 創建新的組件(重要:新的裝配)與真正的實現IExcel的
- 實現工廠類具有「創建」,這將決定哪一個應當返還和決心(或測試)
捕獲異常大會:MyFacade
// the interface
public interface IExcel
{
bool IsAvailable { get; }
// your stuff
}
// the fake implementation
public class FakeExcel: IExcel
{
public IsAvailable { get { return false; } }
// your stuff should probalby throw NotSupportedException
}
大會:MyImplementation
// real implementation
public class RealExcel: IExcel
{
private bool? _isAvailable;
public bool IsAvailable
{
// return value if it is already known, or perform quick test
get { return (_isAvailable = _isAvailable ?? PerformQuickTest()); }
}
private bool PerformQuickTest()
{
try
{
// ... do someting what requires Excel
// it will just crash when it cannot be found/doesn't work
}
catch // (Exception e)
{
return false;
}
return true;
}
}
Assembly:MyFacadeFactory
public class ExcelFactory
{
public static IExcel Create()
{
// delay resolving assembly by hiding creation in another method
return Try(NewRealExcel) ?? new FakeExcel();
}
private static IExcel Try(Func<IExcel> generator)
{
try
{
var result = generator();
if (result.IsAvailable)
return result;
}
catch // (Exception e)
{
// not interested
}
return null; // didn't work exception or IsAvailable returned 'false'
}
// this could be implemented as delegate but it's
// safer when we put NoInlining on it
[MethodImpl(MethodImplOptions.NoInlining)]
private static IExcel NewRealExcel()
{
return new RealExcel();
}
}
會發生什麼?
- 如果您有Excel和MyImplementation組件可以被發現,就會被加載,RealExcel類將被創建,然後使用
- 如果你沒有Excel,但你確實有MyImplementation組件,這將是將會創建RealExcel類,但會在'PerformQuickTest'上失敗,所以將使用FakeExcel
- 如果MyImplementation程序集找不到(您沒有包含它),則在MyFacade中創建RealExcel時將失敗,因此FakeExcel將會已使用
您可以對照用動態加載和反射來完成所有這些事情(少量代碼行),但使用起來有點笨重。我發現這種方法最沒有反射。
如何在代碼的頂部使用引用? using Excel = Microsoft.Office.Interop.Excel; 它不會導致在他的電腦上沒有office和excel的用戶的程序集引用錯誤嗎? – selmar 2013-05-02 17:14:03
@selmar:裝配僅在使用時才加載。 「使用」是不夠的。實際上您需要枚舉或實例化程序集中的類型。 – 2013-05-02 18:11:46