将项目转换为.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();
}
答案 0 :(得分:1)
您应该可以使用FuncConvert.ToFSharpFunc
,这似乎仍然存在于FSharp.Core的netstandard版本中。
答案 1 :(得分:1)
您可以轻松定义自己的:
let ToFSharpFunc (converter: System.Func<_, _>) = fun t -> converter.Invoke t
另外,如果您需要在C#中使用FSharpFunc
,只需在.Invoke
上调用.Invoke
,FSharpFunc
对象上的Func
为{{1}}