如何在 where 子句中使用泛型创建泛型方法? (像泥一样清澈的人!)

发布于 2024-10-09 18:35:48 字数 480 浏览 5 评论 0原文

有没有办法做到这一点:

protected void SubscribeToEvent<TEvent, TPayload>(Action<TPayload> a_action)
    where TEvent : CompositePresentationEvent<TPayload>
{
    TEvent newEvent = _eventAggregator.GetEvent<TEvent>();
    SubscriptionToken eventToken = newEvent.Subscribe(a_action);

    _lstEventSubscriptions.Add(new KeyValuePair<EventBase, SubscriptionToken>(newEvent, eventToken));
}

不需要用户指定 TPayload 参数?

Is there a way of doing this:

protected void SubscribeToEvent<TEvent, TPayload>(Action<TPayload> a_action)
    where TEvent : CompositePresentationEvent<TPayload>
{
    TEvent newEvent = _eventAggregator.GetEvent<TEvent>();
    SubscriptionToken eventToken = newEvent.Subscribe(a_action);

    _lstEventSubscriptions.Add(new KeyValuePair<EventBase, SubscriptionToken>(newEvent, eventToken));
}

without requiring the user to specify a TPayload parameter?

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

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

发布评论

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

评论(2

给不了的爱 2024-10-16 18:35:48

不,没有。 C# 4 中不行。您无法使用单一方法来做到这一点。

无法从传递给方法的参数推断出 TEvent,并且由于部分推断不适用于泛型类型参数,因此您必须在调用站点手动指定类型参数。

No, there isn't. Not in C# 4. You can't do that with a single method.

TEvent cannot be inferred from the arguments passed to the method, and since partial inference is not available for generic type arguments, you'll have to manually specify the type argument at call site.

猥︴琐丶欲为 2024-10-16 18:35:48

不管你相信与否,VB.NET扩展方法 与此非常相似。

假设我有以下扩展方法:

<Extension()>
Function Append(Of TInferred, T)(ByVal source As IEnumerable(Of TInferred)) As Object()
    Dim items As List(Of Object) = source.Cast(Of Object).ToList()

    Dim value As T = Nothing ' equivalent to default(T) '
    items.Add(value)

    Return items.ToArray()
End Function

那么我实际上可以调用此方法,传递单个类型参数,仅适用于T,并推断出TInferred

Dim strings = New String() {"A", "B", "C"}

' Notice I am passing only one type argument. '
Dim objects = strings.Append(Of Integer)()

For Each obj As Object In objects
    Console.WriteLine(obj)
Next

输出:

A
B
C
0

遗憾的是,正如 Mehrdad 所指出的,这在 C# 中是不可能的。

Believe it or not, VB.NET has something very much like this for extension methods.

Say I have the following extension method:

<Extension()>
Function Append(Of TInferred, T)(ByVal source As IEnumerable(Of TInferred)) As Object()
    Dim items As List(Of Object) = source.Cast(Of Object).ToList()

    Dim value As T = Nothing ' equivalent to default(T) '
    items.Add(value)

    Return items.ToArray()
End Function

Then I could actually call this method passing a single type parameter, for T only, with TInferred being inferred:

Dim strings = New String() {"A", "B", "C"}

' Notice I am passing only one type argument. '
Dim objects = strings.Append(Of Integer)()

For Each obj As Object In objects
    Console.WriteLine(obj)
Next

Output:

A
B
C
0

Sadly, as Mehrdad has indicated, this is not possible in C#.

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