带有 (Of T as What?) 的通用函数接受字符串、整数、日期时间?
我有这个函数:
Public Class QueryStringUtil
Public Shared Function GetQueryStringValue(Of T As Structure)(ByVal queryStringVariable As String) As T
Dim queryStringObject As Nullable(Of T) = Nothing
If queryStringVariable <> Nothing Then
If HttpContext.Current.Request.QueryString(queryStringVariable) IsNot Nothing Then
queryStringObject = CTypeDynamic(Of T)(queryStringVariable)
End If
End If
Return queryStringObject
End Function
End Class
当我尝试像这样调用它时:
Dim intTest As Integer = QueryStringUtil.GetQueryStringValue(Of Integer)("stuff")
Dim stringTest As String = QueryStringUtil.GetQueryStringValue(Of String)("stuff")
Dim datetimeTest As DateTime = QueryStringUtil.GetQueryStringValue(Of DateTime)("stuff")
stringTest 给出错误:
'String' 不满足类型参数 'T' 的 'Structure' 约束。
我想要我们的其他开发人员在调用此函数时不必担心必须将类转换为结构或类似的东西。我只是希望他们能够在 (Of T) 中放入标准类型并使其工作。我不介意编写额外的计算来实现这一点。
另一个问题是我还需要该函数能够返回实际的 null 值,因此我需要将 queryStringObject 作为 Nullable(Of T)。这意味着我必须将 T 作为结构,否则它告诉我这是行不通的。所以看起来如果我改变 T 是什么,我需要运行一些计算来将 var 删除为空或不可空。
编辑:
我尝试重载此方法,以便一个返回 T ,一个返回 Nullable(Of T) ,如下所示:
Public Shared Function GetQueryStringValue(Of T As Class)(ByVal queryStringVariable As String) As T
Public Shared Function GetQueryStringValue(Of T As Structure)(ByVal queryStringVariable As String) As Nullable(Of T)
自然地,它告诉我它不能这样做,因为它们只是返回类型不同。无论如何要超载这个吗?我真的不想有两个功能。
I've got this Function:
Public Class QueryStringUtil
Public Shared Function GetQueryStringValue(Of T As Structure)(ByVal queryStringVariable As String) As T
Dim queryStringObject As Nullable(Of T) = Nothing
If queryStringVariable <> Nothing Then
If HttpContext.Current.Request.QueryString(queryStringVariable) IsNot Nothing Then
queryStringObject = CTypeDynamic(Of T)(queryStringVariable)
End If
End If
Return queryStringObject
End Function
End Class
When I try to call it like this:
Dim intTest As Integer = QueryStringUtil.GetQueryStringValue(Of Integer)("stuff")
Dim stringTest As String = QueryStringUtil.GetQueryStringValue(Of String)("stuff")
Dim datetimeTest As DateTime = QueryStringUtil.GetQueryStringValue(Of DateTime)("stuff")
stringTest gives me the error:
'String' does not satisfy the 'Structure' constraint for type parameter 'T'.
I want our other developers to not worry about having to convert a class to a structure or some stuff like that when they call this function. I just want them to be able to put a standard type in the (Of T) and have it work. I don't mind writing in extra calculations to achieve that.
The other problem is I also need the function to be able to return an actual null value, so I kind of need the queryStringObject as Nullable(Of T). Which means I have to have T as Structure otherwise it tells me that won't work. So looks like if I change what T is I need to run some calculation to delcare the var as nullable or not.
EDIT:
I tried overloading this method so that one returns T and one returns Nullable(Of T) like so:
Public Shared Function GetQueryStringValue(Of T As Class)(ByVal queryStringVariable As String) As T
Public Shared Function GetQueryStringValue(Of T As Structure)(ByVal queryStringVariable As String) As Nullable(Of T)
And naturally it's telling me it can't do that since they just differ by return types. Is there anyway to overload this? I really don't want to have two functions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为
String
不能为 null,并且不是Structure
或值类型。它是一个引用类型。您应该只具有一些返回
String
和Nullable
版本的重载。Its because
String
can't be nullable and isn't aStructure
or a value type. It is a reference type.You should just have some overloads that returns a
String
andNullable
versions.尽管这可能没有帮助,但据我所知,没有办法用泛型来解决它。
为什么不直接使用
Object
呢?是性能问题吗?As unhelpful as this may be, AFAIK there's no way to get around it with generics.
Why not just work with
Object
? Is it a performance issue?