在C#中使用F#选项类型
我有以下类型:
and ListInfo() = let mutable count = 0 // This is a mutable option because we can't have an infinite data structure. let mutable lInfo : Option = 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
其中递归“列表信息”是一个选项。 有一个或没有。 我需要从C#中使用它,但我得到错误。 这是一个示例用法:
if (FSharpOption.get_IsSome(listInfo.LInfo)) { Types.ListInfo subListInfo = listInfo.LInfo.Value; HandleListInfo(subListInfo, n); }
这里listInfo的类型为ListInfo,如上所示。 我只是想检查它是否包含值,如果是,我想使用它。 但是所有访问listInfo.LInfo都会给出错误“语言不支持属性,索引器或事件listInfo.LInfo ……”
谁知道为什么?
我怀疑问题是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