从传递到C#的F#列表检索项列表

2023-09-03 06:30:32 作者:蛋定的人生不需要解释

我有被称为在F#在C#中的函数,传递它的参数 Microsoft.FSharp.Collections.List<对象>

I have a function in C# that is being called in F#, passing its parameters in a Microsoft.FSharp.Collections.List<object>.

我如何能够获得在C#函数从F#列表中的项目?

How am I able to get the items from the F# List in the C# function?

修改

我已经找到了功能性风​​格的方式,通过他们循环,并可以将它们传递给函数如下返回C#System.Collection.List:

I have found a 'functional' style way to loop through them, and can pass them to a function as below to return C# System.Collection.List:

private static List<object> GetParams(Microsoft.FSharp.Collections.List<object> inparams)
{
    List<object> parameters = new List<object>();
    while (inparams != null)
    {
        parameters.Add(inparams.Head);
        inparams = inparams.Tail;
     }
     return inparams;
 }

再次编辑

的F#列表,正如指出的下面,是可枚举,因此,上述功能可以被替换的行;

The F# List, as was pointed out below, is Enumerable, so the above function can be replaced with the line;

new List<LiteralType>(parameters);

有什么办法,但是,通过索引引用一个项目的F#列表?

Is there any way, however, to reference an item in the F# list by index?

推荐答案

在一般情况下,避免暴露F#的特异类型(如F#'名单'型),以其他语言,因为经验是不是所有的伟大的(如你可以看到)。

In general, avoid exposing F#-specific types (like the F# 'list' type) to other languages, because the experience is not all that great (as you can see).

这是F#列表是一个IEnumerable,所以你可以创建如从这种方式pretty的动辄System.Collections.Generic.List。

An F# list is an IEnumerable, so you can create e.g. a System.Collections.Generic.List from it that way pretty easily.

目前没有有效的索引,因为它是一个单链接列表等访问任意元素为O(n)。如果你想要那个索引,改变为另一种数据结构是最好的。

There is no efficient indexing, as it's a singly-linked-list and so accessing an arbitrary element is O(n). If you do want that indexing, changing to another data structure is best.