VB.Net泛型约束等效于C#,其中TValue:New()

问题描述 投票:0回答:1

我正在将C#中的扩展方法转换为VB.Net,但无法找出对输出设置约束的正确方法。

我当前的C#代码正在使用Dictionary,外观如下:

public static TValue GetOrCreate<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key)
    where TValue : new()
{   }

我看过一些问题,解释了如何在inputs上设置约束,例如here,但是我似乎找不到一个解释如何转换where TValue : new()output约束​​的问题。

我确信这很容易,但是我对VB有点生疏,希望这里的人能从头顶上了解到它。

c# vb.net generics extension-methods
1个回答
5
投票

这是从Telerik的Code Converter中获得的转换:

Module Extensions
  <Extension()>
  Public Function GetOrCreate(Of TKey, TValue As New)(ByVal dict As IDictionary(Of TKey, TValue), ByVal key As TKey) As TValue

  End Function
End Module
© www.soinside.com 2019 - 2024. All rights reserved.