【问题标题】:Can a delegate declaration inherit from another delegate declaration?委托声明可以从另一个委托声明继承吗?
【发布时间】:2011-12-06 20:49:58
【问题描述】:

有没有一种方法可以在不再次输入整个签名的情况下编写以下内容?

//desired base signature
    public delegate string BaseDelegate<TProfile, TResult>(string requestorID, DateTime sentDate, string serviceID,
        string source, TProfile profile, out DateTime recieved, out DateTime sent, out string psatSystemID, out TResult[] result);

//ugly version of child
public delegate string CurriedDelegate<T>(string requestorId, DateTime sentDate, string serviceId, string source,
T profile, out DateTime recieved, out DateTime sent, out string psatSystemID, out T[] result);

//syntax sugar,doesn't compile
    public BaseDelegate<T,T> CurriedDelegate<T>; //TProfile is same type as TResult

【问题讨论】:

    标签: .net generics .net-4.0 delegates


    【解决方案1】:

    不,没有办法这样做,尽管如果两个类型参数相同,您可以从 BaseDelegate&lt;TProfile, TResult&gt; 创建一个 CurriedDelegate&lt;T&gt;

    在我看来,更好的解决方案是将各种参数封装在单独的类型中。这确实是一个非常长的签名,并且可能参数彼此相关的。

    (我也会尽量避免使用这么多 out 参数 - 也许您实际上已经在这里封装了 两种 类型,一种用于输入,一种用于输出?)

    【讨论】:

    • 是的,我调用的代码超出了项目此阶段的清理范围。据我所知,在大多数情况下,98% 的服务层甚至都使用了 2 个参数。当您说我可以创建CurriedDelegate&lt;T&gt; 时,您的意思是实例化,而不是定义短/甜类型签名,对吗?在>80% 的服务层代码TProfileTResult 是相同的。
    • @Maslow:是的,如果你已经有BaseDelegate&lt;T1, T2&gt;,你可以使用new CurriedDelegate&lt;Whatever&gt;(existingDelegate)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多