发布一个F#类实现一个接口时,反射/ C#的输入错误 [英] Reflection / C# typing errors when publishing an F# class implementing an interface

查看:80
本文介绍了发布一个F#类实现一个接口时,反射/ C#的输入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C#编写的接口,但在F#中实现它的时候,我注意到一些怪事。

I have an interface written in C#, but when implementing it in F#, I noticed some oddities.


  • 的F#类已被转换为界面之前,C#可以使用它

  • 铸造后,WPF无法读取它的属性(绑定失败,SNOOP无法反映吧)

  • 我可以换用C#code中的对象,一切工作正常。

接口

public interface IInterpret {
  public string Name {get;}
  public IEnumberable<Project> Interpret(string text);
}

的F#类

type Interpreter()=
  interface IInterpret with
    member x.Name = "FParsec Based"
    member x.Interpret(str) = Seq.empty

的code以下无法编译
误差在国米preTER不执行IInterpert

The code below fails to compile The error is about Interpreter not implementing IInterpert

public ViewModel(){
  IInterpret i = new FSharpLib.Interperter(); 
}

这是我目前的解决方法

public class MyProxy: IInterpret{
    private IInterpret _cover;

    public MyProxy()        {
        _cover = new FSharpLib.Interperter() as IInterpret;
    }
    public string Name { get { return _cover.Name; } }
    public IEnumerable<Project> Interpret(string text){
        return _cover.Interpret(text);
    }
}

有什么我做错了我的F#类闪避,或代理需要的?我使用的是当前的VS2010 F#,不带外下降。

Is there something I'm doing wrong with my F# class def, or is the proxy needed? I'm using the current VS2010 f#, not an out of band drop.

推荐答案

F#明确地实现了所有接口。这意味着你必须明确地转换为接口类型。

F# implements all interfaces explicitly. This means you must explicitly cast to the interface type.

我不知道一吨约WPF绑定到明确的接口,但看到如果这些

I don't know a ton about WPF binding to explicit interfaces, but see if these

http://leecampbell.blogspot.com/2008/09/generic-binding-in-wpf-through-explicit.html

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/92a2a3ba-74a6-4c79-9c75-f42d232a4dbf

帮助? (我只是发现他们炳ING为WPF结合明确的接口。)

help? (I just found them Bing-ing for "wpf binding explicit interfaces".)

(另一种选择可能是做这样的事情。

(Another alternative may be to do something like

type Interpreter()= 
    member x.Name = "FParsec Based" 
    member x.Interpret(str:string) = Seq.empty 
    interface IInterpret with 
        member x.Name = x.Name
        member x.Interpret(str) = x.Interpret(str)

在这里你明确实现接口含蓄,如果你原谅令人混淆的术语。)

where you "explicitly implement the interface implicitly", if you pardon the confusing terminology.)

这篇关于发布一个F#类实现一个接口时,反射/ C#的输入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆