[英]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
ISparqlCommand
為ISparqlCommand
才能訪問已實現的成員(不是您所問的問題,但很容易因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.