处理从类型对象缩小到类型字符串的错误消息

Dealing with error message narrowing from type object to type string

我在将代码从 .Net 2.0 转换为 .Net 4.5 时收到此错误消息:

Option strict on disallows narrowing from type 'object' to type 'string' in copying the value of 'ByRef' parameter 'ParamValue' back to the matching argument.

代码如下所示:

Public Shared Function TheFunction(ByRef x As Object ) As Integer
    TheFunction = 5
    // ultimately called like this: SqlCommand.Parameters.AddWithValue("field", x)
End Function

Private Function AFunction(ByVal x As String) As Boolean

   Dim cnt As Integer = TheFunction(x)

End Function

我用谷歌搜索了答案,似乎建议是更改 TheFunction

我受限,无法改变TheFunction

我可以关闭严格,但我宁愿对这个问题进行一个很好的修复,比如将 x 复制到另一个变量并将该变量传入。

这行得通吗?

Dim boxedObject as Object = CType(x, Object)
Dim cnt As Integer = TheFunction(boxedObject)
x = CType(boxedObject, String)