如何使 VB.NET 函数的参数作为泛型类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25255014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:02:09  来源:igfitidea点击:

How to make Parameters of VB.NET function as Generic type?

vb.netfunctiongenericsgeneric-programming

提问by N.T.C

I have a VB.NET function as below, the parameter 'x' that is passed to the function is of Type 'Single'. However, I want to write the function so that it can accept any numeric type such as 'Single', 'Double' and 'Integer'. I know one way of doing that is to write 3 functions with the same names, but it would be so tedious. Can anyone suggest any idea? Thank you.

我有一个 VB.NET 函数,如下所示,传递给该函数的参数“x”的类型为“Single”。但是,我想编写函数以便它可以接受任何数字类型,例如“单”、“双”和“整数”。我知道这样做的一种方法是编写 3 个具有相同名称的函数,但这会很乏味。任何人都可以提出任何想法吗?谢谢你。

Public Function Square(x As Single) As Single
  Return x * x
End Function

回答by Shell

try following method

试试下面的方法

Public Function Square(Of T)(ByVal x As Object) As T
    Dim b As Object = Val(x) * Val(x)
    Return CType(b, T)
End Function

You can use above function like this

您可以像这样使用上述功能

Dim p As Integer = Square(Of Integer)(10)
Dim d As Double = Square(Of Double)(1.5)

回答by Bj?rn-Roger Kringsj?

You can constrain the generic type by IConvertibleand Structure. The following data types implements the IConvertible interface:

您可以通过IConvertible和来约束泛型类型Structure。以下数据类型实现了 IConvertible 接口:

  • System.Boolean
  • System.Byte
  • System.Char
  • System.DateTime
  • System.DBNull
  • System.Decimal
  • System.Double
  • System.Enum
  • System.Int16
  • System.Int32
  • System.Int64
  • System.SByte
  • System.Single
  • System.String
  • System.UInt16
  • System.UInt32
  • System.UInt64
  • 系统布尔值
  • 系统字节
  • 系统字符
  • 系统日期时间
  • System.DBNull
  • 系统.十进制
  • 双系统
  • 系统枚举
  • System.Int16
  • System.Int32
  • System.Int64
  • 系统字节
  • 系统单机
  • 系统字符串
  • System.UInt16
  • System.UInt32
  • 系统.UInt64

Here's a rewrite of the code found in the link provided by SLaks:

这是对 SLaks 提供的链接中的代码的重写:

Public Function Square(Of T As {IConvertible, Structure})(x As T) As T
    'TODO: If (GetType(T) Is GetType(Date)) Then Throw New InvalidOperationException()
    Dim left As ParameterExpression = Expression.Parameter(GetType(T), "x")
    Dim right As ParameterExpression = Expression.Parameter(GetType(T), "x")
    Dim body As BinaryExpression = Expression.Multiply(left, right)
    Dim method As Func(Of T, T, T) = Expression.Lambda(Of Func(Of T, T, T))(body, left, right).Compile()
    Return method(x, x)
End Function

Reference: https://jonskeet.uk/csharp/miscutil/usage/genericoperators.html

参考https: //jonskeet.uk/csharp/miscutil/usage/genericoperators.html

回答by Tripweed

The code allows any value to posted via Arg but only numeric values will be processed. The returned value must be double because Val returns double only.

该代码允许通过 Arg 发布任何值,但只会处理数值。返回值必须是双精度值,因为 Val 只返回双精度值。

The Of T allows for generic object types to be presented.

Of T 允许呈现通用对象类型。

   Private Function sqr(Of T)(Arg As T) As Double
    If IsNumeric(Arg) Then
        Return Val(Arg) * Val(Arg)
    Else
        Return 0
    End If

  End Function