【发布时间】:2011-11-21 17:01:53
【问题描述】:
使用 C# 2.0,我可以像这样指定默认参数值:
static void Test([DefaultParameterValueAttribute(null)] String x) {}
由于此 C# 4.0 语法不可用:
static void Test(String x = null) {}
那么,值类型是否有 C# 2.0 等效项?例如:
static void Test(int? x = null) {}
以下尝试无法编译。
// error CS1908: The type of the argument to the DefaultValue attribute must match the parameter type
static void Test([DefaultParameterValueAttribute(null)] int? x) {}
// error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression
static void Test([DefaultParameterValueAttribute(new Nullable<int>())] int? x) {}
【问题讨论】:
-
这是为了开发一个供外部语言调用的库吗?
-
是的,它是一个图书馆。该代码实际上是使用 CSharpCodeProvider 生成的,并且只是调用到专有 C++ 变体类的 C++/CLI 接口。 “企业”。