C# shortcut for #if ... #else ... #endif like #define something as string(#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# 快捷方式


基础教程推荐
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01