我实际上正在做这样的事情。大会被探查,如果他们存在/将工作。
您可以在许多方面,当然,做到这一点,但是这就是我结束了:
- 提取这个类到接口的所有功能(我们称之为: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