如何在f#中实现c#接口?

时间:2022-09-02 00:27:03

I would like to implement the following C# interface in F#:

我想在f#中实现以下c#接口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mono.Addins;

[TypeExtensionPoint]
public interface ISparqlCommand
{
    string Name { get; }
    object Run(Dictionary<string, string> NamespacesDictionary,    org.openrdf.repository.Repository repository, params object[] argsRest);
}   

This is what I have tried, but it gives me: "Incomplete structured construct at or before this point in expression"

这就是我所尝试的,但它给了我:“在这个表达式点之前或之前的不完整结构”

#light

module Module1

open System
open System.Collections.Generic;

type MyClass() =
    interface ISparqlCommand with
        member this.Name = 
            "Finding the path between two tops in the Graph"
        member this.Run(NamespacesDictionary, repository, argsRest) = 
            new System.Object

What am I doing wrong? Maybe indentation is wrong?

我做错了什么?也许缩进是错误的?

2 个解决方案

#1


3  

I verified @Mark's answer in the comments. Given the following C# code:

我在评论中验证了@Mark的回答。给出以下c#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace org.openrdf.repository {
    public class Repository {
    }
}

namespace CSLib
{

    [System.AttributeUsage(System.AttributeTargets.Interface)]
    public class TypeExtensionPoint : System.Attribute
    {
        public TypeExtensionPoint()
        {
        }
    }


    [TypeExtensionPoint]
    public interface ISparqlCommand
    {
        string Name { get; }
        object Run(Dictionary<string, string> NamespacesDictionary, org.openrdf.repository.Repository repository, params object[] argsRest);
    }

}

The following F# implementation (only change is adding () at the object construction) works "fine":

下面的f#实现(唯一的改变是在对象构造中添加()))工作“很好”:

#light

module Module1

open System
open System.Collections.Generic;
open CSLib

type MyClass() =
    interface ISparqlCommand with
        member this.Name = 
            "Finding the path between two tops in the Graph"
        member this.Run(NamespacesDictionary, repository, argsRest) = 
            new System.Object()

Though you don't need to use #light anymore (it is the default) and you may want to head the NamespaceDictionary paramater name warning that "Uppercase variable identifiers should not generally be used in patterns, and may indicate a misspelt pattern name." Also note that you will need to cast MyClass to ISparqlCommand in order to access the implemented members (not a question you asked, but easy to get confused by coming from C#): e.g (MyClass() :> ISparqlCommand).Name

虽然您不再需要使用#light(它是默认的),但是您可能需要将名称空间字典参数名放在前面,警告“大写变量标识符一般不应该在模式中使用,并可能指示一个拼写错误的模式名。”还要注意,为了访问实现的成员,您需要将MyClass强制转换为ISparqlCommand(而不是您所问的问题,但是很容易从c#中获得):e。g(MyClass():> ISparqlCommand). name

#2


1  

Thanks to everyone! The following code actually works:

感谢大家!下面的代码实际上是有效的:

namespace MyNamespace

open System
open System.Collections.Generic;
open Mono.Addins

[<assembly:Addin>]
    do()
[<assembly:AddinDependency("TextEditor", "1.0")>]
    do()

[<Extension>]
type MyClass() =
    interface ISparqlCommand with
         member this.Name
             with get() = 
                 "Finding the path between two tops in a Graph"
         member this.Run(NamespacesDictionary, repository, argsRest) = 
             new System.Object()

It is also an example of how to use Mono.Addins with F#

这也是如何使用Mono的一个例子。外接程序和f#

#1


3  

I verified @Mark's answer in the comments. Given the following C# code:

我在评论中验证了@Mark的回答。给出以下c#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace org.openrdf.repository {
    public class Repository {
    }
}

namespace CSLib
{

    [System.AttributeUsage(System.AttributeTargets.Interface)]
    public class TypeExtensionPoint : System.Attribute
    {
        public TypeExtensionPoint()
        {
        }
    }


    [TypeExtensionPoint]
    public interface ISparqlCommand
    {
        string Name { get; }
        object Run(Dictionary<string, string> NamespacesDictionary, org.openrdf.repository.Repository repository, params object[] argsRest);
    }

}

The following F# implementation (only change is adding () at the object construction) works "fine":

下面的f#实现(唯一的改变是在对象构造中添加()))工作“很好”:

#light

module Module1

open System
open System.Collections.Generic;
open CSLib

type MyClass() =
    interface ISparqlCommand with
        member this.Name = 
            "Finding the path between two tops in the Graph"
        member this.Run(NamespacesDictionary, repository, argsRest) = 
            new System.Object()

Though you don't need to use #light anymore (it is the default) and you may want to head the NamespaceDictionary paramater name warning that "Uppercase variable identifiers should not generally be used in patterns, and may indicate a misspelt pattern name." Also note that you will need to cast MyClass to ISparqlCommand in order to access the implemented members (not a question you asked, but easy to get confused by coming from C#): e.g (MyClass() :> ISparqlCommand).Name

虽然您不再需要使用#light(它是默认的),但是您可能需要将名称空间字典参数名放在前面,警告“大写变量标识符一般不应该在模式中使用,并可能指示一个拼写错误的模式名。”还要注意,为了访问实现的成员,您需要将MyClass强制转换为ISparqlCommand(而不是您所问的问题,但是很容易从c#中获得):e。g(MyClass():> ISparqlCommand). name

#2


1  

Thanks to everyone! The following code actually works:

感谢大家!下面的代码实际上是有效的:

namespace MyNamespace

open System
open System.Collections.Generic;
open Mono.Addins

[<assembly:Addin>]
    do()
[<assembly:AddinDependency("TextEditor", "1.0")>]
    do()

[<Extension>]
type MyClass() =
    interface ISparqlCommand with
         member this.Name
             with get() = 
                 "Finding the path between two tops in a Graph"
         member this.Run(NamespacesDictionary, repository, argsRest) = 
             new System.Object()

It is also an example of how to use Mono.Addins with F#

这也是如何使用Mono的一个例子。外接程序和f#