2

我在 c# 中编写了一些使用库的代码,但我想分享它并希望它能够工作,无论该库是否存在,基本上我想检查我的代码是否存在库,如果它不存在,我使用备用代码来完成库应该做的事情。

那么你会怎么做呢?

我想也许我可以使用预处理器指令,但老实说,我对这些几乎没有经验,似乎无法找到如何做到这一点。

编辑 :

刚刚发现已经问过一个类似的问题: Checking for the exist of a reference/type at compile time in .NET (我在发布之前实际上已经搜索过,但不知何故错过了这个)

但似乎没有一个令人满意的答案。

真的没有办法做到这一点吗?

Edit2: 很抱歉没有早点指定,但我写的代码是在Unity3D项目中使用的,它基本上是一堆脚本。

4

1 回答 1

0

Let's assume your project is called A:

  1. Create a new project, B, and then add the sources files from A to B (see http://blogs.msdn.com/b/saraford/archive/2008/11/26/did-you-know-how-to-add-a-linked-item-to-a-project-365.aspx) for details of how to do this.

  2. In project A's properties, under Build add a Conditional compilation symbol, eg USE_LIBRARY.

  3. For each source file that uses the library, modify the using statements to something like:

    #if USE_LIBRARY

    using Lib.Xxx

    #else

    using DummyLib.Xxx

    #endif

Then create DummyLib with the alternative code.

That way A.dll will be dependent on the library and B.dll won't be. You can then distribute B.dll without the library.

于 2013-09-27T10:49:01.697 回答