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# 快捷方式
基础教程推荐
- rabbitmq 的 REST API 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01