在c#中使用f#选项类型

本文关键字:选项 类型 | 更新日期: 2023-09-27 18:18:35

我有以下类型:

and ListInfo() =
let mutable count = 0
// This is a mutable option because we can't have an infinite data structure.
let mutable lInfo : Option<ListInfo> = None
let dInfo = new DictInfo()
let bInfo = new BaseInfo()
member this.BaseInfo = bInfo
member this.DictInfo = dInfo
member this.LInfo
    with get() = lInfo
    and set(value) = lInfo <- Some(value)
member this.Count
    with get() = count
    and set(value) = count <- value

,其中递归的"list info"是一个选项。要么有,要么没有。我需要使用这个从c#,但我得到错误。下面是一个示例用法:

if (FSharpOption<Types.ListInfo>.get_IsSome(listInfo.LInfo))
{
    Types.ListInfo subListInfo = listInfo.LInfo.Value;
    HandleListInfo(subListInfo, n);
}

这里的listInfo和上面一样是listInfo类型。我只是想检查它是否包含一个值如果是,我想使用它。但是所有的访问都是listInfo。LInfo给出错误"属性、索引器或事件listInfo"。语言不支持LInfo…"

有人知道为什么吗?

在c#中使用f#选项类型

我怀疑问题是LInfo属性的getter/setter使用不同的类型(c#中不支持)。

试试这个

member this.LInfo
    with get() = lInfo
    and set value = lInfo <- value

member this.LInfo
    with get() = match lInfo with Some x -> x | None -> Unchecked.defaultof<_>
    and set value = lInfo <- Some value