繁体   English   中英

如何在F#中实现C#接口?

[英]How to implement a 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);
}   

这是我尝试过的方法,但是它给了我:“在表达式的这一点或之前不完整的结构化构造”

#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

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

我在评论中验证了@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);
    }

}

以下F#实现(仅在对象构造处更改为add () )有效:

#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()

尽管您不再需要使用#light (它是默认设置),并且您可能想要在NamespaceDictionary参数名中添加#light警告:“大写变量标识符通常不应在模式中使用,并且可能表示拼写错误的模式名称。” 还要注意,您需要将MyClass ISparqlCommandISparqlCommand才能访问已实现的成员(不是您所问的问题,但很容易因C#而感到困惑):例如(MyClass() :> ISparqlCommand).Name

谢谢大家! 以下代码实际上有效:

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()

这也是如何在F#中使用Mono.Addins的示例

暂无
暂无

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

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