【问题标题】:VB.Net Generics Constraints Equivalent to C# where TValue : New()VB.Net 泛型约束等效于 C# 其中 TValue : New()
【发布时间】:2020-02-11 16:49:33
【问题描述】:

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

我当前的 C# 代码正在处理字典,如下所示:

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

我已经看到一些问题解释了如何对 输入 设置约束,例如 here,但我似乎找不到解释如何转换 输出 的问题strong> where TValue : new() 的约束

我确信这很容易,但我对 VB 有点生疏了,希望这里有人知道。

【问题讨论】:

标签: c# vb.net generics extension-methods


【解决方案1】:

这是从 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

【讨论】:

  • 领先我大约 30 秒。
  • 哇哦!!! - 诀窍是GetOrCreate(Of TKey, TValue As New) - 谢谢!!!
  • 我看到了人们的 cmets,但没有一个有意义/有效 - 这给了我答案。谢谢!!
  • 另外:有一个编辑删除了 Shared 修饰符,我回滚了。 Shared 如果方法是模块的一部分,则应该删除,但如果方法是类的一部分,则仍然需要。由于这段代码是从 C# 转换而来的,我相信后者的可能性更大。
  • @AhmedAbdelhameed 已经单独输入了回复,只需要一分钟就可以准备好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多