2010-12-29 33 views
5

Tôi muốn thực hiện các giao diện # C sau trong F #:Làm cách nào để triển khai giao diện C# trong F #?

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); 
} 

Đây là những gì tôi đã cố gắng, nhưng nó mang lại cho tôi: "xây dựng cấu trúc Incomplete tại hoặc trước thời điểm này trong khái niệm"

#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 

Tôi đang làm gì sai? Có thể thụt lề là sai?

+6

Có lẽ chỉ thiếu dấu ngoặc trên 'System.Object mới() '? –

+1

Giao diện C# là gì? Bạn đã xác định một giao diện CLR trong C#. –

Trả lời

3

Tôi đã xác minh câu trả lời của @ Mark trong các nhận xét. Với C# mã sau:

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); 
    } 

} 

Sau đây F # thực hiện (chỉ thay đổi được thêm () ở việc xây dựng đối tượng) hoạt động "tốt":

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

Mặc dù bạn không cần phải sử dụng #light nữa (đó là mặc định) và bạn có thể muốn cảnh báo tên người tham dự NamespaceDictionary rằng "Số nhận dạng biến hoa thường không được sử dụng trong các mẫu và có thể chỉ ra tên mẫu viết sai chính tả". Cũng lưu ý rằng bạn sẽ cần phải truyền MyClass tới ISparqlCommand để truy cập vào các thành viên được triển khai (không phải là câu hỏi mà bạn đã hỏi, nhưng dễ nhầm lẫn khi đến từ C#): ví dụ: (MyClass() :> ISparqlCommand).Name

1

Xin cảm ơn tất cả mọi người! Các mã sau đây thực sự hoạt động:

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

Đây cũng là một ví dụ về cách sử dụng Mono.Addins với F #

Các vấn đề liên quan