2010-12-29 14 views
5

me gustaría poner en práctica el siguiente # interfaz de C en C#:¿Cómo implementar una interfaz 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); 
} 

Esto es lo que he intentado, pero me da: "constructo estructurado incompleto en o antes de este punto en la expresión"

#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é estoy haciendo mal? ¿Quizás la sangría es incorrecta?

+6

Quizás sólo falta el parens en 'nueva System.Object()'? –

+1

¿Qué es una interfaz C#? Ha definido una interfaz CLR en C#. –

Respuesta

3

Comprobé la respuesta de @ Mark en los comentarios. Dado el siguiente código 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); 
    } 

} 

La siguiente F # aplicación (único cambio es la adición de () en la construcción de objetos) funciona "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() 

A pesar de que no es necesario utilizar #light más (es el valor predeterminado) y es posible que desee encabezar el nombre del parámetro NamespaceDictionary advirtiendo que "los identificadores de mayúsculas en general no se deben usar en patrones, y pueden indicar un nombre de patrón de error". También tenga en cuenta que se necesita para echar a MyClassISparqlCommand con el fin de acceder a los miembros implementadas (no es una pregunta que hizo, pero fácil confundirse al venir desde C#): por ejemplo (MyClass() :> ISparqlCommand).Name

1

Gracias a todos! El siguiente código funciona realmente:

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

También es un ejemplo de cómo utilizar Mono.Addins con F #

Cuestiones relacionadas