Generic Function with (Of T as What?) to accept String, Integer, DateTime?

北战南征 提交于 2020-01-06 03:03:09

问题


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.


回答1:


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?

Public Shared Function GetQueryStringValue(ByVal queryStringVariable As String, ByVal t As Type) As Object
    Dim queryStringObject As Object = Nothing
    If queryStringVariable <> Nothing Then
        If HttpContext.Current.Request.QueryString(queryStringVariable) IsNot Nothing Then
            queryStringObject = CTypeDynamic(queryStringVariable, t)
        End If
    End If

    Return queryStringObject
End Function



回答2:


Its because String can't be nullable and isn't a Structure or a value type. It is a reference type.

You should just have some overloads that returns a String and Nullable versions.



来源:https://stackoverflow.com/questions/6792050/generic-function-with-of-t-as-what-to-accept-string-integer-datetime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!