带有 (Of T as What?) 的通用函数接受字符串、整数、日期时间?

发布于 2024-11-25 10:12:27 字数 1559 浏览 1 评论 0原文

我有这个函数:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那片花海 2024-12-02 10:12:28

这是因为 String 不能为 null,并且不是 Structure 或值类型。它是一个引用类型。

您应该只具有一些返回 StringNullable 版本的重载。

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.

柳若烟 2024-12-02 10:12:28

尽管这可能没有帮助,但据我所知,没有办法用泛型来解决它。

为什么不直接使用Object呢?是性能问题吗?

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文