是否可以将预编译符号保留在C#程序集中以供引用程序使用? [英] Is it possible to keep precompiled symbols in C# assemblies for use in a referencing program?

查看:172
本文介绍了是否可以将预编译符号保留在C#程序集中以供引用程序使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道C#预处理程序通常会消除预编译的符号。规则可以被打破吗?

I know that the C# preprocessor will generally eliminate the precompiled symbols. Can the rule be broken a bit?

例如,我有一个C#dll A ,我想在另一个C#程序 B 中使用它。在 B 中,我有不同的预编译符号 X Y Z 。如果定义了 X ,则需要一个 A 版本。否则,如果定义了 Y ,则需要另一个版本的 A 。与 Z 相似。为此,我想在 A 的源代码中编写预编译的指令,例如

For example, I have a C# dll A, which I wanna use in another C# program B. In B I have different precompiled symbols X, Y, Z. If X is defined, I need a version of A. Else if Y is defined, I need another version of A. And similar for Z. To implement this, I want to write precompiled directives in A's source code, like

public static class MyClass
{
    public static void MyMethod()
    {
#if X
        DoX();
#elif Y
        DoY();
#elif Z
        DoZ();
#else
        DoWhatever();
#endif
    }
}

我希望这些情况不要在 A 的编译过程中不会消失, B 可以使用它们。可能吗?还是我应该做些什么来解决此问题?

I hope these conditions don't disappear during A's compiling and B can take use of them. Is it possible? Or what should I do to work around this?

预先感谢。

编辑

一些背景信息。 B 是Unity3D游戏,而 A 是我们要在不同游戏中使用的通用库。因此, A B 都依赖于程序集UnityEngine.dll。然后,U3D定义了各种预编译符号来指示当前的构建平台,引擎的版本等,我想在 A B 中都使用它们。 / p>

Some background information. B is a Unity3D Game, and A is a common library we want to use across different games. So Both A and B relies on the assembly UnityEngine.dll. Then there is various precompiled symbols defined by U3D to indicate the current build platform, the engine's version and etc, which I want to take use of in both A and B.

推荐答案

通常,您基于项目定义编译器常量。因此,如果您的共享程序集定义了 X 并进行了编译,则它仅包含对 DoX()的调用。然后,在定义 Y 的地方编译实际使用的应用程序,但这不会影响共享程序集。

In general, you define compiler constants on a project basis. So if your shared assembly defines X and you compile it, it only contains the call to DoX(). Then you compile the actually consuming application where you define Y, but that won't affect the shared assembly.

但是,可以使用命令行或XAML将编译器常量传递给编译器,然后将其应用于同时构建的所有项目。

However, compiler constants can be passed to the compiler using the command line or using XAML, and then apply to all projects that are built at the same time.

是否可以在解决方案基础上#define常量? MSDN:如何:声明条件编译常量

因此,这可能是一个选择:在解决方案范围内声明它们并整体编译解决方案。请注意,共享程序集仅包含与在编译时定义的常量相对应的调用:您不能与使用不同常量编译的不同可执行文件共享该程序集,因为只有 DoX()如果在编译时声明了 X ,将调用

So that might be an option: declare them solution-wide and compile the solution as a whole. Do note that your shared assembly then only contains the call corresponding to the constant(s) defined at compile-time: you can't share that assembly with different executables compiled with different constants, because only DoX() will be called if X was declared at compile-time.

或者,相反,您完全放弃了这个想法并实现了正确的设计模式,因此将根据调用者想要的东西来调用正确的方法。 >,而不是谁是呼叫者。

Or, rather, you dump this idea altogether and implement the proper design pattern, so the proper method will be called depending on what the caller wants, not who the caller is.

这可能意味着您应该只公开 DoX() DoY() DoZ()给调用者,或者让 MyMethod ()根据传递的(枚举)变量执行某项操作,或使用策略模式

This may mean you should simply expose DoX(), DoY() and DoZ() to the caller, or let MyMethod() do something based on a passed (enum) variable, or something using a strategy pattern.

话虽这么说,可能有理由去做您想做的事情,例如,不要将实施细节泄漏给竞争者使用您的应用程序。

That being said, there may be reasons to do what you're trying to do, for example not leaking implementation details to competing companies who use your application.

这篇关于是否可以将预编译符号保留在C#程序集中以供引用程序使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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