我想要做的是创建一个带#if #else的用户定义类型,以便在代码的后面,我可以在进行变量声明时引用该类型.一个简单的例子如下
#define INT_ONLY
//#define LONG_ONLY
#if INT_ONLY
using type_var as int;
#else
using type_var as long;
#endif
private sub main() {
type_var x = 0;
}
Run Code Online (Sandbox Code Playgroud)
如果允许这种类型的操作,我寻找的是正确的语法.
您正在寻找的是以下内容
#if INT_ONLY
using type_var = System.Int32;
#else
using type_var = System.Int64;
#endif
Run Code Online (Sandbox Code Playgroud)
虽然我很好奇你为什么要这样做?它似乎会产生很多用户混淆.我认为更好的解决方案是创建一个包装器类型,它使用#ifdef一个实现细节隐藏它的存储类型(很多方式IntPtr)
struct type_var {
#if INT_ONLY
int m_value;
#else
long m_value;
#endif
public type_var(int i) {
m_value = i;
}
#if INT_ONLY
public type_var(long l) {
m_value = l;
}
#endif
public static operator type_var(int i) {
return new type_var(i);
}
#if INT_ONLY
public static operator type_var(long i) {
return new type_var(i);
}
#endif
// Etc ...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
176 次 |
| 最近记录: |