简体   繁体   English

在F#中使用绑定接口

[英]Using bound interface in F#

I am trying to use C# library in F# so it would be very much specific case. 我试图在F#中使用C#库,所以这将是非常具体的情况。 I using Servicestack with F#. 我使用Servicestack和F#。 Now, I am trying to wire up class with interface using method 现在,我正在尝试使用接口使用方法连接类

RegisterAutoWiredAs<T,'TAs>()

signature. 签名。 Here is 'T is having constraint that it has to implement 'TAs. 这是'T有约束,它必须实现'TA。 It works fine in C# code. 它在C#代码中工作正常。

But F# is having constraint while using interface. 但是F#在使用界面时有限制。

let f:IFoo = Foo() // will give type error
let fi:IFoo - Foo() :> IFoo // will work

Here Foo has implemented IFoo. 在这里,Foo实施了IFoo。 So, this part is quite different than C# equivalent. 所以,这部分与C#等价物完全不同。 Now, above signature is giving type error if I do like this 现在,如果我这样做,上面的签名就会给出类型错误

container.RegisterAutoWiredAs<Foo,IFoo>()

And there is noway to do casting while giving parameter. 现在有了在给出参数的同时进行铸造。

Here is line from original project I am trying to run. 这是我试图运行的原始项目的 Everything in this code works other than this part and also sadly other equivalent methods are also failing. 此代码中的所有内容都与此部分不同,而且遗憾的是其他等效方法也失败了。

And here is the error I am getting in that project 这是我在该项目中遇到的错误

This expression was expected to have type 'MemoryChatHistory' but here has type 'IChatHistory' 该表达式预计具有“MemoryChatHistory”类型,但此处的类型为“IChatHistory”

F# does not support implicit interface implementations. F#不支持隐式接口实现。

I think you may be able to work around this in this instance by making IChatHistory an abstract class rather than an interface (using [<AbstractClass>] attribute). 我想你可以通过使IChatHistory成为一个抽象类而不是一个接口(使用[<AbstractClass>]属性)来解决这个问题。

EDIT: 编辑:

Nope, I had a chance to play around with it today. 不,我今天有机会玩弄它。 I think it's impossible to call this method directly with those type parameters from F#. 我认为用F#中的那些类型参数直接调用这个方法是不可能的。 See this question 看到这个问题

How do I translate a `where T : U` generic type parameter constraint from C# to F#? 如何将`where T:U`泛型类型参数约束从C#转换为F#?

for a little more discussion. 再讨论一下。

You might be able to work around this by using reflection to call the method. 您可以通过使用反射来调用该方法来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM