#if ... #else ... #endif like #define something as string 的 C# 快捷方式

C# shortcut for #if ... #else ... #endif like #define something as string(#if ... #else ... #endif like #define something as string 的 C# 快捷方式)

本文介绍了#if ... #else ... #endif like #define something as string 的 C# 快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Mono 的 C# dotNET 中,有没有更简单的方法?

In C# dotNET for Mono, is there an easier way of doing this?

#if __MonoCS__
    public static SqliteConnection NewConnection
#else
    public static SQLiteConnection NewConnection
#endif

在 C 语言中,我可以先 #if 然后 #define 某些东西,#else 并将其定义为其他东西.

In C I would be able to #if and then #define something, #else and define it as something else.

我知道 C# 预处理器不允许我想要的,但是有没有更简单的方法来处理 SQLite for Windows 和 Linux 之间的差异?

I know that the C# preprocessor doesn't allow what I want, but is there a simpler way of coping with the differences between SQLite for Windows and Linux?

谢谢,
吉姆

感谢那些回答的人.
依赖 SQLite 的文件现在在开头包含这样的语句:

Thanks to those who answered.
Files that depend on SQLite now contain statements like this at the beginning:

#if __MonoCS__
    using Mono.Data.Sqlite;
    using SQLiteCommand =     Mono.Data.Sqlite.SqliteCommand;
    using SQLiteConnection =  Mono.Data.Sqlite.SqliteConnection;
    using SQLiteException =   Mono.Data.Sqlite.SqliteException;
    using SQLiteParameter =   Mono.Data.Sqlite.SqliteParameter;
    using SQLiteTransaction = Mono.Data.Sqlite.SqliteTransaction;
#else
    using System.Data.SQLite;
#endif

在 C 语言中,我会将所有内容放在一个 .h 文件中,并在需要的地方 #include .有没有办法在 Mono C# 项目中做到这一点?我试过的都没有用.

In C I would put all that in a .h file and #include it where needed. Is there a way to do that in a Mono C# project? Nothing I tried worked.

再次感谢,
吉姆

Thanks again,
Jim

推荐答案

好吧,你可以使用 using alias 指令:

Well, you can use a using alias directive:

#if __MonoCS__
using SQLiteConnection = Foo.Bar.SqliteConnection;
#endif

然后你可以在同一个文件中的任何地方使用 SQLiteConnection.

Then you can use SQLiteConnection everywhere within the same file.

您可能希望使用适配器模式将其仅放在一段代码中,因此您的所有其余代码都可以相同.

You may want to use the adapter pattern to put this just in a single piece of code though, so all the rest of your code can be the same either way.

这篇关于#if ... #else ... #endif like #define something as string 的 C# 快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:#if ... #else ... #endif like #define something as string 的 C# 快捷方式

基础教程推荐