2010-12-29 6 views
5

Je voudrais implémenter l'interface C# suivante F #:Comment implémenter une interface C# en 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); 
} 

C'est ce que je l'ai essayé, mais il me donne: « construction incomplète structuré ou avant ce point dans l'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 

Qu'est-ce que je fais de mal? Peut-être que l'indentation est fausse?

+6

Peut-être manque juste les parens sur 'nouveau System.Object()'? –

+1

Qu'est-ce qu'une interface C#? Vous avez défini une interface CLR en C#. –

Répondre

3

J'ai vérifié la réponse de Mark dans les commentaires. Compte tenu du code C# suivant:

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

} 

La mise en œuvre F # suivant (seul changement est l'ajout () à la construction de l'objet) fonctionne "bien":

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

Bien que vous n'avez pas besoin d'utiliser plus #light (il s'agit de la valeur par défaut) et vous pouvez indiquer le nom du paramètre NamespaceDictionary en indiquant que «les identificateurs de variable en majuscules ne doivent généralement pas être utilisés dans les modèles et peuvent indiquer un nom de modèle mal orthographié». Notez également que vous aurez besoin de jeter MyClass-ISparqlCommand afin d'accéder aux membres mis en œuvre (pas une question que vous avez posée, mais facile de se confondre en venant de C#): (MyClass() :> ISparqlCommand).Name

par exemple
1

Merci à tous! Le code suivant fonctionne réellement:

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

Il est aussi un exemple de la façon d'utiliser Mono.Addins avec F #

Questions connexes