扩展方法和类型约束

发布于 2024-09-18 12:20:19 字数 808 浏览 10 评论 0原文

我开始使用扩展方法,遇到了这个问题: 在下一个场景中,我得到一个:

“扩展方法具有永远无法满足的类型约束”

Public Interface IKeyedObject(Of TKey As IEquatable(Of TKey))
    ReadOnly Property InstanceKey() As TKey 
End Interface

<Extension()> _
Public Function ToDictionary(Of TKey As IEquatable(Of TKey), tValue As IKeyedObject(Of TKey))(ByVal l As IEnumerable(Of tValue)) As IDictionary(Of TKey, tValue)
     'code
End Function

但是,如果我用 IKeyedObject(Of Integer) 替换 IKeyedObject(Of k) ,它就可以工作,

  <Extension()> _
    Public Function ToDictionary(Of TKey As IEquatable(Of TKey), tValue As IKeyedObject(Of TKey))(ByVal l As IEnumerable(Of tValue)) As IDictionary(Of TKey, tValue)
        'code
    End Function

我错过了什么吗?有什么办法我可以在这里做我想做的事吗?

提前致谢

I am starting to play with extension methods and i came across with this problem:
In the next scenario i get a:

"extension method has a type constraint that can never be satisfied"

Public Interface IKeyedObject(Of TKey As IEquatable(Of TKey))
    ReadOnly Property InstanceKey() As TKey 
End Interface

<Extension()> _
Public Function ToDictionary(Of TKey As IEquatable(Of TKey), tValue As IKeyedObject(Of TKey))(ByVal l As IEnumerable(Of tValue)) As IDictionary(Of TKey, tValue)
     'code
End Function

But it works if i replace IKeyedObject(Of k) with IKeyedObject(Of Integer)

  <Extension()> _
    Public Function ToDictionary(Of TKey As IEquatable(Of TKey), tValue As IKeyedObject(Of TKey))(ByVal l As IEnumerable(Of tValue)) As IDictionary(Of TKey, tValue)
        'code
    End Function

Am i missing something? is any way i can do what i want here??

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

飘落散花 2024-09-25 12:20:19

扩展方法“”具有永远无法满足的类型约束。

我已阅读以下有关此错误的内容 MSDN

由于该方法是扩展方法,编译器必须能够仅根据方法声明中的第一个参数来确定该方法扩展的数据类型 [.. .]

在您的情况下,编译器必须能够从参数 l 推断出 TKeyTValue,这是不可能的。因此编译器会发出警告。


哪一种是有道理的。毕竟,想象一下您将如何调用扩展方法:

Dim values As IEnumerable(Of TValue) = ...

Dim dictionary As IDictionary(Of ?, TValue) = values.ToDictionary()
'                                ^                                            '
'                                where does the compiler get this type from?  '

诚然,编译器可以通过让您显式声明另一个类型参数来推断它,就像 values.ToDictionary(Of TKey)() ,但显然它不允许这样做。

Extension method '<methodname>' has type constraints that can never be satisfied.

I've read the following about this error on MSDN:

Because the method is an extension method, the compiler must be able to determine the data type or types that the method extends based only on the first parameter in the method declaration [...]

In your case, the compiler would have to be able to deduce both TKey and TValue from parameter l, which is not possible. Thus the compiler warning.


Which sort-of makes sense. After all, imagine how you're going to call your extension method:

Dim values As IEnumerable(Of TValue) = ...

Dim dictionary As IDictionary(Of ?, TValue) = values.ToDictionary()
'                                ^                                            '
'                                where does the compiler get this type from?  '

Admittedly, the compiler could deduce the other type parameter by letting you state it explicitly, à la values.ToDictionary(Of TKey)(), but apparently it doesn't allow this.

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