#if ...#else ...#endif之类的C#快捷方式#将某些内容定义为字符串 [英] C# shortcut for #if ... #else ... #endif like #define something as string
问题描述
在用于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#预处理器不允许我想要的东西,但是有没有更简单的方法来应对Windows和Linux的SQLite之间的差异?
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别名指令:
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之类的C#快捷方式#将某些内容定义为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!