Using F# Option Type in C#

I have the following type:

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

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);
}

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..."

Anyone that understands why?


I suspect the problem is the LInfo property getter/setter work with different types (which isn't supported in C#).

Try this

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

Or this

member this.LInfo
    with get() = match lInfo with Some x -> x | None -> Unchecked.defaultof<_>
    and set value = lInfo <- Some value
链接地址: http://www.djcxy.com/p/10254.html

上一篇: 为Django项目配置Pylint

下一篇: 在C#中使用F#选项类型