模型的定义:
type ItemEvent =
| ItemCreated of ItemCreated
and ItemCreated = { timestamp:DateTime; id:ItemId; name:string; description:string option } interface IEvent
这是我要调用的方法的接口(interface):
type IItemWriteAccess =
abstract Update: ItemEvent -> Result<unit, DomainMessage>
这是 C# 代码:
public class ItemEventHandlers
{
private readonly IItemWriteAccess _repository;
public ItemEventHandlers(IItemWriteAccess repository)
{
_repository = repository;
}
public void Handle(ItemCreated msg)
{
if(msg == null) throw new ArgumentNullException("msg");
_repository.Update(msg); // this doesn't work
}
}
这会产生以下错误消息:
Argument 1: cannot convert from 'DomainModels.ItemCreated' to 'DomainModels.ItemEvent'
向上转换到 ItemEvent
也不起作用。
我该如何解决?
最佳答案
由于 Update
采用 ItemEvent
,因此不能将 ItemCreated
实例作为参数传递。
我必须创建一个 ItemEvent
并将 ItemCreated
实例传递给构造函数,如下所示:
var @event = ItemEvent.NewItemCreated(msg);
然后我可以将它传递给 Update
:
public void Handle(ItemCreated msg)
{
if(msg == null) throw new ArgumentNullException("msg");
var @event = ItemEvent.NewItemCreated(msg);
_repository.Update(@event);
}
关于c# - 如何将区分联合从 C# 传递到 F# 函数(F# - C# 互操作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32581216/