没有使用泛型扩展方法的类型推断

时间:2011-08-24 05:57:54

标签: c# .net generics extension-methods fluent-interface

我有以下方法:

public static TEventInvocatorParameters Until
    <TEventInvocatorParameters, TEventArgs>(this TEventInvocatorParameters p,
                                            Func<TEventArgs, bool> breakCond)
    where TEventInvocatorParameters : EventInvocatorParameters<TEventArgs>
    where TEventArgs : EventArgs
{
    p.BreakCondition = breakCond;
    return p;
}

这个班级

public class EventInvocatorParameters<T>
    where T : EventArgs
{
    public Func<T, bool> BreakCondition { get; set; }
    // Other properties used below omitted for brevity.
}

现在,我遇到以下问题:

  1. 此扩展方法适用于所有类型,甚至string
  2. 我无法写new EventInvocatorParameters<EventArgs>(EventABC).Until(e => false);它告诉我“方法的类型参数......无法从用法中推断出来。”
  3. 我不能使用像这样的泛型类型参数吗?你怎么解决这个问题?
    重要的一点:我需要这两个泛型参数,因为我需要返回调用此扩展方法的相同类型。


    更广泛的图片(无需回答问题!):
    我正在尝试创建一个流畅的接口来调用事件。基础是这个静态类:

    public static class Fire
    {
       public static void Event<TEventArgs>(
           ConfiguredEventInvocatorParameters<TEventArgs> parameters)
        where TEventArgs : EventArgs
        {
            if (parameters.EventHandler == null)
            {
                return;
            }
    
            var sender = parameters.Sender;
            var eventArgs = parameters.EventArgs;
            var breakCondition = parameters.BreakCondition;
    
            foreach (EventHandler<TEventArgs> @delegate in 
                     parameters.EventHandler.GetInvocationList())
            {
                try
                {
                    @delegate(sender, eventArgs);
                    if (breakCondition(eventArgs))
                    {
                        break;
                    }
                }
                catch (Exception e)
                {
                    var exceptionHandler = parameters.ExceptionHandler;
                    if (!exceptionHandler(e))
                    {
                        throw;
                    }
                }
            }
        }
    }
    

    要确保只能使用完全配置的参数调用此方法,它只接受ConfiguredEventInvocatorParameters<T>派生的EventInvocatorParameters<T>

    public class ConfiguredEventInvocatorParameters<T>
        : EventInvocatorParameters<T>
        where T : EventArgs
    {
        public ConfiguredEventInvocatorParameters(
            EventInvocatorParameters<T> parameters, object sender, T eventArgs)
            : base(parameters)
        {
            EventArgs = eventArgs;
            Sender = sender;
        }
    
        public T EventArgs { get; private set; }
        public object Sender { get; private set; }
    
    }
    

    以下是有效的电话:

    Fire.Event(EventName.With(sender, eventArgs));
    Fire.Event(EventName.With(sender, eventArgs).Until(e => e.Cancel));
    Fire.Event(EventName.Until(e => e.Cancel).With(sender, eventArgs));
    

    以下内容无效:

    // no sender or eventArgs have been specified, i.e. missing call to With(...)
    Fire.Event(EventName.Until(e => e.Cancel));
    

    为了使这项工作,存在名为With的扩展方法,它接受EventHandler<TEventArgsTEventInvocatorParameters并返回ConfiguredEventInvocatorParameters<TEventArgs>。现在With之后的所有调用也需要返回类型ConfiguredEventInvocatorParameters<TEventArgs>,否则有效调用的第二个示例(最后使用Until)将无效。
    如果您对API有任何想法,请告诉我。但是,我想避免以下三件事:

    • 如果尚未完全配置参数,则仅在运行时失败
    • 创建像EventName.With(...).Until(...).Fire()
    • 这样的反向语法
    • 使用臭名昭着的Do方法启动:Fire(EventName).With(...).Until(...).Do();

4 个答案:

答案 0 :(得分:19)

通用方法类型推断故意从约束中进行任何推断。相反,从参数形式参数中进行推导,然后根据约束检查推导出的类型参数。

有关约束和方法签名的一些设计问题的详细讨论,包括几十个人告诉我,我认为现有的设计是明智的是错误的,请参阅我关于这个主题的文章:

http://blogs.msdn.com/b/ericlippert/archive/2009/12/10/constraints-are-not-part-of-the-signature.aspx

答案 1 :(得分:3)

对于任何有兴趣的人,现在,我用通用的类层次结构解决了原始问题(流畅的事件调用API)。这基本上是Hightechrider对类固醇的回答。

public abstract class EventInvocatorParametersBase
    <TEventInvocatorParameters, TEventArgs>
    where TEventArgs : EventArgs
    where TEventInvocatorParameters :
        EventInvocatorParametersBase<TEventInvocatorParameters, TEventArgs>

{
    protected EventInvocatorParametersBase(
        EventHandler<TEventArgs> eventHandler,
        Func<Exception, bool> exceptionHandler,
        Func<TEventArgs, bool> breakCondition)
    {
        EventHandler = eventHandler;
        ExceptionHandler = exceptionHandler;
        BreakCondition = breakCondition;
    }

    protected EventInvocatorParametersBase(
        EventHandler<TEventArgs> eventHandler)
        : this(eventHandler, e => false, e => false)
    {
    }

    public Func<TEventArgs, bool> BreakCondition { get; set; }
    public EventHandler<TEventArgs> EventHandler { get; set; }
    public Func<Exception, bool> ExceptionHandler { get; set; }

    public TEventInvocatorParameters Until(
        Func<TEventArgs, bool> breakCondition)
    {
        BreakCondition = breakCondition;
        return (TEventInvocatorParameters)this;
    }

    public TEventInvocatorParameters WithExceptionHandler(
        Func<Exception, bool> exceptionHandler)
    {
        ExceptionHandler = exceptionHandler;
        return (TEventInvocatorParameters)this;
    }

    public ConfiguredEventInvocatorParameters<TEventArgs> With(
        object sender, 
        TEventArgs eventArgs)
    {
        return new ConfiguredEventInvocatorParameters<TEventArgs>(
            EventHandler, ExceptionHandler, BreakCondition,
            sender, eventArgs);
    }
}

public class EventInvocatorParameters<T> :
    EventInvocatorParametersBase<EventInvocatorParameters<T>, T>
    where T : EventArgs
{
    public EventInvocatorParameters(EventHandler<T> eventHandler)
        : base(eventHandler)
    {
    }
}

public class ConfiguredEventInvocatorParameters<T> :
    EventInvocatorParametersBase<ConfiguredEventInvocatorParameters<T>, T>
    where T : EventArgs
{
    public ConfiguredEventInvocatorParameters(
        EventHandler<T> eventHandler,
        Func<Exception, bool> exceptionHandler,
        Func<T, bool> breakCondition, object sender,
        T eventArgs)
        : base(eventHandler, exceptionHandler, breakCondition)
    {
        EventArgs = eventArgs;
        Sender = sender;
    }

    public ConfiguredEventInvocatorParameters(EventHandler<T> eventHandler,
                                              object sender,
                                              T eventArgs)
        : this(eventHandler, e => false, e => false, sender, eventArgs)
    {
    }

    public T EventArgs { get; private set; }
    public object Sender { get; private set; }
}

public static class EventExtensions
{
    public static EventInvocatorParameters<TEventArgs> Until<TEventArgs>(
        this EventHandler<TEventArgs> eventHandler,
        Func<TEventArgs, bool> breakCondition)
        where TEventArgs : EventArgs
    {
        return new EventInvocatorParameters<TEventArgs>(eventHandler).
            Until(breakCondition);
    }

    public static EventInvocatorParameters<TEventArgs> 
        WithExceptionHandler<TEventArgs>(
            this EventHandler<TEventArgs> eventHandler,
            Func<Exception, bool> exceptionHandler)
        where TEventArgs : EventArgs
    {
        return
            new EventInvocatorParameters<TEventArgs>(eventHandler).
                WithExceptionHandler(exceptionHandler);
    }

    public static ConfiguredEventInvocatorParameters<TEventArgs>
        With<TEventArgs>(
            this EventHandler<TEventArgs> eventHandler, object sender,
            TEventArgs eventArgs)
        where TEventArgs : EventArgs
    {
        return new ConfiguredEventInvocatorParameters<TEventArgs>(
            eventHandler, sender, eventArgs);
    }
}

这允许您编写如下代码:

Fire.Event(EventName.WithExceptionHandler(e => false)
                    .Until(e => false).With(this, EventArgs.Empty));
Fire.Event(EventName.With(this, EventArgs.Empty));
Fire.Event(EventName.WithExceptionHandler(e => false)
                    .With(this, EventArgs.Empty).Until(e => false));
Fire.Event(EventName.With(this, EventArgs.Empty)
                    .WithExceptionHandler(e => false).Until(e => false));

但它不允许你写这个,因为并没有提供所有必要的信息(eventArgs和sender):

Fire.Event(EventName.Until(e => false));
Fire.Event(EventName);

答案 2 :(得分:1)

需要使用扩展方法是否有某些原因?如果您将Until放在EventInvocatorParameters<T>课程上,则可以避免上述两个问题:

public class EventInvocatorParameters<T>
    where T : EventArgs
{
    public Func<T, bool> BreakCondition { get; set; }
    // Other properties used below omitted for brevity.

    public EventInvocatorParameters<T> Until (Func<T, bool> breakCond)
    {
        this.BreakCondition = breakCond;
        return this;
    }
}

答案 3 :(得分:0)

我知道一点警察,但您是否考虑过使用Rx,而不是重新发明您似乎想要做的事情?