简体   繁体   English

F#:使用C#中的F#索引属性

[英]F#: Using a F# indexed property from C#

I have written a class in F# implementing an interface in order to build a C#-friendly interface for my F#-assembly. 我已经在F#中编写了一个实现接口的类,以便为我的F#程序集构建一个C#友好的接口。

I have written some of the properties as indexed properties. 我已经写了一些属性作为索引属性。 However, when I try to use the type from C#, I only get the synthetic get_PropertyName methods in intellisense and the compiler likewise complains in case I want to use the indexed properties like I would do for C# ones. 但是,当我尝试使用C#中的类型时,我只会在intellisense中获得综合的get_PropertyName方法,并且如果我想像我对C#那样使用索引属性,编译器也会抱怨。

Code for reference: 参考代码:

type IMyInterfaceType =   
    abstract MyProperty : MyType1 with get
    abstract MyIndexedProperty : MyType2 -> MyType3 with get
    abstract MyTwoDimensionalIndexedProperty : (MyType4 * MyType5) -> MyType6 with get

type MyInterfaceType =   

    new () = { }

    interface IMyInterfaceType with
        member this.MyProperty with get () = new MyType1 ()
        member this.MyIndexedProperty with get parameter = new MyType3 ()
        member this.MyTwoDimensionalIndexedProperty with get pair = new MyType6 ()

When trying to access this class from C#, I only get methods 尝试从C#访问此类时,我仅获取方法

get_MyIndexedProperty(MyType2 parameter)
get_MyTwoDimensionalIndexedProperty(Tuple<MyType4, MyType5>)

instead of the indexed properties I had hoped for. 而不是我希望的索引属性。

Am I doing something wrong or is this a known issue? 我是在做错事还是已知问题?

cheers 干杯
--Mathias. --Mathias。

Response to the original question : 回答原始问题

Indexer properties in C# have special name Item thus to create indexer accessible from C# you must name your indexer property "Item", eg: C#中的索引器属性具有特殊的名称Item因此要创建可从C#访问的索引器,必须将索引器属性命名为“ Item”,例如:

type X () =
    member this.Item with get key = ....

Now it can be accessed both in F# using (x : X).[key] or in C# using x[key] . 现在,既可以使用(x : X).[key]在F#中访问它,也可以使用(x : X).[key]在C#中对其进行访问。

Response to the updated question : 对更新后的问题的回答

C# does not support indexed properties the way F# does. C#不像F#那样支持索引属性。 Instead using additional type is advised: https://msdn.microsoft.com/en-us/library/aa288464%28v=vs.71%29.aspx 建议改用其他类型: https : //msdn.microsoft.com/zh-cn/library/aa288464%28v=vs.71%29.aspx

So you can try to use something like this: 因此,您可以尝试使用以下方法:

[<AbstractClass>]
type Indexer<'index, 'result> () =
    abstract Get : 'index -> 'result
    member this.Item with get key = this.Get key

type IMyInterfaceType =   
    abstract MyProperty : MyType1 with get
    // F# indexed propetties
    abstract MyIndexedProperty : MyType2 -> MyType3 with get
    // C# workaround
    abstract MyCSharpIndexedProperty : Indexer<MyType2, MyType3> with get

type MyInterfaceType () as this =
    let proxy =
      { new Indexer<MyType2, MyType3> () with
          member __.Get key = (this :> IMyInterfaceType).MyIndexedProperty key }
    interface IMyInterfaceType with
        member __.MyProperty with get () = new MyType1 ()
        member __.MyIndexedProperty with get key = new MyType3 ()
        member __.MyCSharpIndexedProperty with get () = proxy

And two dimensional property similarly by creating Indexer<'index1, 'index2, 'result> () = ... 通过创建Indexer<'index1, 'index2, 'result> () = ...类似地获得二维属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM