netstandard20中的FSharp.Core FSharpFunc.FromConverter

时间:2018-01-05 18:28:14

标签: f# .net-standard-2.0

将项目转换为.NET Standard 2.0时,FSharpFunc<T, TResult>FromConverter库中的FSharp.Core方法不再可用。有没有办法将此代码转换为.NET Standard 2.0 FSharp.Core实现?

async Task<Unit> RunProcess(FSharpMailboxProcessor<T> mailbox, Func<T, Task> process) { ... }

public BaseMailboxProcessor(Func<TFuncInput, Task> process, Action<Exception, TFuncInput> errorHandler = null, CancellationToken? cancellationToken = null)
{
    m_ErrorHandler = errorHandler;
    m_TokenSource = cancellationToken.HasValue ? CancellationTokenSource.CreateLinkedTokenSource(cancellationToken.Value) :
                                                 new CancellationTokenSource();
    var cancellationOption = FSharpOption<CancellationToken>.Some(m_TokenSource.Token);
    Converter<FSharpMailboxProcessor<TMailboxInput>, FSharpAsync<Unit>> converter = (mailbox) =>
    {
        return FSharpAsync.AwaitTask<Unit>(RunProcess(mailbox, process));
    };

    m_Mailbox = new FSharpMailboxProcessor<TMailboxInput>(FSharpFunc<FSharpMailboxProcessor<TMailboxInput>, FSharpAsync<Unit>>.FromConverter(converter), cancellationOption);
    m_Mailbox.Start();
}

2 个答案:

答案 0 :(得分:1)

您应该可以使用FuncConvert.ToFSharpFunc,这似乎仍然存在于FSharp.Core的netstandard版本中。

答案 1 :(得分:1)

您可以轻松定义自己的:

let ToFSharpFunc (converter: System.Func<_, _>) = fun t -> converter.Invoke t

另外,如果您需要在C#中使用FSharpFunc,只需在.Invoke上调用.InvokeFSharpFunc对象上的Func为{{1}}