这是问题“Can CodeDom create optional arguments when generating a c# method?”的表弟
我尝试了there给出的答案。
但是,当我尝试编译时,我收到以下错误:
错误BC30455:未为'公共功能栏(optionalParam As Integer)的参数'optionalParam'指定参数作为整数
我已经将它提炼到不支持OptionalAttribute,DefaultParameterValueAttribute或两者的Visual Basic编译器。
这是我正在编译的已提炼代码:
Imports System.Runtime.InteropServices
Namespace SSI.RuntimeGenerated.FunctionsNamespace
Public Class Functions
Public Function Foo() As Integer
return Bar()
End Function
Public Function Bar( _
<[Optional], DefaultParameterValue(1)> _
ByVal optionalParam As Integer) _
As Integer
return optionalParam
End Function
End Class
End Namespace
使用以下命令进行编译:
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /t:library /out:foobar.dll foobar.vb /langversion:11
产生以下输出:
Microsoft (R) Visual Basic Compiler version 11.0.50709.17929
Copyright (c) Microsoft Corporation All rights reserved.
C:\<snip>\foobar.vb : error BC30455: Argument not specified for parameter
'optionalParam' of 'Public Function Bar(optionalParam As Integer) As Integer'.
return Bar()
~~~~~
如果我手动将方法签名更改为
Public Function Bar(Optional ByVal optionalParam As Integer) As Integer
然后编译得很好。
所以我的问题是:
答案 0 :(得分:1)
VB.NET不支持OptionalAttribute
。我找不到任何具体说明的官方文档,但如果你试图在VB.NET项目中使用它,它将没有任何效果。要在VB.NET中创建可选参数,必须使用Optional
关键字for instace:
Public Class Functions
Public Function Foo() As Integer
Return Bar()
End Function
Public Function Bar(Optional ByVal optionalParam As Integer = 1) As Integer
Return optionalParam
End Function
End Class