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

查看:22
本文介绍了是否可以在 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中,我有不同的预编译符号XYZ.如果定义了 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 是我们想要在不同游戏中使用的通用库.所以 AB 都依赖于程序集 UnityEngine.dll.然后是 U3D 定义的各种预编译符号来指示当前构建平台、引擎版本等,我想在 AB 中使用它们.

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 Constant?MSDN:如何to:声明条件编译常量.

How to do so is explained in Is There anyway to #define Constant on a Solution Basis? and MSDN: How to: Declare Conditional Compilation Constants.

所以这可能是一个选项:在解决方案范围内声明它们并编译整个解决方案.请注意,您的共享程序集仅包含与在编译时定义的常量相对应的调用:您不能与使用不同常量编译的不同可执行文件共享该程序集,因为只有 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.

或者,更确切地说,您完全抛弃了这个想法并实现了正确的设计模式,因此将根据调用者想要什么而不是调用者是谁来调用正确的方法em>.

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() 根据传递的(枚举)变量做一些事情,或者使用 strategy 做一些事情模式.

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天全站免登陆