如何检查编程如果一个集引用存在于C#? [英] How to check programatically if an assembly reference exists in c#?

查看:332
本文介绍了如何检查编程如果一个集引用存在于C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个小C#/。NET库非常具体的财务计算。

I am trying to write a small c#/.net library for very specific financial calculations.

我写了仅仅依赖于.NET Framework的标准组件几类。也就是说,这些类的库要求无非.NET框架(4.0客户端)。

I have written several classes that depend only on the standard assemblies of the .net framework. That is, the library with these classes require nothing other than the .net framework (4.0 client).

现在我需要一个额外的类Excel集成。这个类将需要与Microsoft Office相关的附加组件引用和出类拔萃,就像他们各自的对象库。

Now I need an additional class for excel integration. This class will require additional assembly references related with microsoft office and excel, like their respective object libraries.

我的问题是:一些这个库的用户可能有办公室和擅长的,有些不是。

My problem is: Some of the users of this library may have office and excel, some not.

我如何添加这个额外的类图书馆,使这两种类型的用户可以使用该库没有得到错误?

How can I add this additional class to the library so that both types of users can use the library without getting errors?

更多precisely:如果用户没有安装Office和Excel,用户必须能够运行所有不包括Excel的相关的一个没有得到错误的类

More precisely: If a user does not have office and excel, user must be able to run all the classes excluding excel-related one without getting errors.

感谢您的帮助,selmar

thanks for any help, selmar

推荐答案

我其实是做这样的事情。如果它们存在/会工作组件进行探测。

I was actually doing something like this. Assemblies were probed if they exist/will work.

您可以在许多方面,当然,做到这一点,但这是我最终什么用:

You can do this in many ways, of course, but that's what I ended with:

  • 在这个类到界面中提取所有功能(我们称之为:IExcel),并加入IsAvailable属性,它
  • 实施假类实现IExcel,并从IsAvailable返回假(当然,其他方法抛出NotSupportedException异常)。
  • 创建新的组件(重要的是:新的装配)与IExcel真正落实
  • 实施工厂类创建,这将决定哪一个应返回和决心(或测试)catch异常

大会:MyFacade

Assembly: 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

Assembly: 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;
    }
}

大会:MyFacadeFactory

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();
    }
}

会发生什么?

What will happen?

  • 如果您有Excel和MyImplementation组件可以发现,它会被加载,RealExcel类将被创建,然后用
  • 如果您没有Excel,但你确实有MyImplementation组件,它会被加载,RealExcel类将被创建,但会失败在PerformQuickTest所以FakeExcel将用于
  • 如果MyImplementation组件无法找到(未包括它)时RealExcel在MyFacade创建它会失败,所以FakeExcel将用于

您当然可以做所有这些事情与动态加载和反射(的code少线),但稍微有点笨重使用。我发现这个方法最反射更少。

You can of course do all those things with dynamic loading and reflection (less lines of code) but a little bit clunky to use. I've found this method most reflection-less.

这篇关于如何检查编程如果一个集引用存在于C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆