c# - 返回带有一个参数的元组作为泛型 : Tuple<bool, T>

标签 c# f# c#-to-f#

我正在学习 F#,但我不知道如何实现这一目标。

我有一个界面:

type IRule =
    abstract member Getrule : keyfield:string -> bool * 'T

在 C# 中:

public interface IRule
{
    Tuple<bool, T> GetRule<T>(string keyFieldIdentifier);
}

我需要创建一个实现此接口(interface)的类

C#:

public class CustomRule : IRule
{
    Tuple<bool, T> IRule.GetRule<T>(string keyFieldIdentifier)
    {
        var _result = SomeHelper.CreateResult(keyFieldIdentifier);
        return new Tuple<bool, T>(_result.Item1, (T)(object)_result.Item2);
    }    
}

F#:

type SomeClass() =
    interface IRule with
        member this.Getrule<'T>(keyfield) = 
            let value = 3
            (true, value :> 'T)

我不知道最后一行(true, value :> 'a)怎么写,如何转换为泛型类型?

错误提示:

错误 1 ​​来自类型的静态强制转换 整数
到 'T
涉及基于该程序点之前的信息的不确定类型。某些类型不允许静态强制转换。

最佳答案

这将编译 - 但我不太确定它是否会做你想要的事情

type SomeClass() =
    interface IRule with
        member this.Getrule<'T>(keyfield):bool * 'T =
            let value = 3
            (true,unbox box value )

特别是,这不会进行强制转换,并且可能在运行时失败。

关于c# - 返回带有一个参数的元组作为泛型 : Tuple<bool, T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21395102/

相关文章:

c# - Roslyn:带有代码修复项目的诊断中的当前工作区

f# - F#中的重复序列

facebook-graph-api - 使用 F# 从 Facebook 图形 API 检索数据

c# - 在 NHibernate 中规范化 EnumStringType

c# - LINQ Where 忽略重音和大小写

f# - 为什么模块与类中相同 F# 函数的类型签名之间存在差异?

f# - 检查几种选项类型,然后转换为类型

generics - F# 泛型类型问题

asynchronous - 从 C# 调用非异步 Func<T> 返回 F# 函数中的启动 Async<T>?

c# - IValidatableObject.Validate() 中的本地化