在C#中使用F#选项类型 [英] Using F# Option Type in C#

查看:91
本文介绍了在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<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

其中递归列表信息"是一个选项.有一个或没有一个.我需要从C#使用它,但出现错误.这是一个示例用法:

where the recursive "list info" is an Option. Either there is one or there is none. I need to use this from C# but I get errors. This is a sample usage:

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

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

here listInfo is of the type ListInfo as above. I'm just trying to check if it contains a value and if so I want to use it. But all the accesses listInfo.LInfo gives the error "Property, indexer or event listInfo.LInfo is not supported by the language..."

知道为什么吗?

推荐答案

我怀疑问题是LInfo属性的getter/setter方法可以用于不同的类型(C#不支持).

I suspect the problem is the LInfo property getter/setter work with different types (which isn't supported in 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

这篇关于在C#中使用F#选项类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆