【问题标题】:How to implement a C# interface in F#?如何在 F# 中实现 C# 接口?
【发布时间】:2010-12-29 17:48:12
【问题描述】:

我想在 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

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

【问题讨论】:

  • 也许只是缺少new System.Object() 上的括号?
  • 什么是 C# 接口?你已经在 C# 中定义了一个 CLR 接口。

标签: c# .net inheritance f#


【解决方案1】:

我在 cmets 中验证了 @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# 实现(唯一的变化是在对象构造中添加 ())工作“很好”:

#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 参数名称警告标题中指出“大写变量标识符通常不应在模式中使用,并且可能表明拼写错误的模式名称。”另请注意,您需要将 MyClass 转换为 ISparqlCommand 才能访问已实现的成员(不是您提出的问题,但来自 C# 很容易混淆):例如 (MyClass() :&gt; ISparqlCommand).Name

【讨论】:

    【解决方案2】:

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

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

    这也是如何将 Mono.Addins 与 F# 一起使用的示例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-14
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多