从C#调用F#`printfn` [英] Call F# `printfn` from C#

查看:64
本文介绍了从C#调用F#`printfn`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何直接从C#代码调用F# printfn ?我看到有一个

How can I call F# printfn directly from C# code? I see there's a

T PrintfModule.PrintFormat<T>(PrintfFormat<T, TextWriter, Unit,Unit> format)

例如,我有定义了 x 的F#程序集:

For example, I have F# assembly that defines x:

type R = { r: R }
let rec x = { r = x }

在另一个C#程序集中,如何通过使用 PrintfModule.PrintFormatLine printfn%A" x 等效的代码来打印 x >?

In another C# assembly, how can I print x by calling code that is equivalent to printfn "%A" x with PrintfModule.PrintFormatLine?

推荐答案

我已经创建了F#文件Program.fs:

I've created F# file Program.fs:

open System

type R = { r: R }
let rec x = { r = x }

[<EntryPoint>]
let main argv =
    printfn "%A" x
    0

编译它,然后反编译为C#:

compiled it and then decompiled to C#:

using Microsoft.FSharp.Core;
...

public static Program.R x {
    get { return \u0024Program.x\u00406; }
}

[EntryPoint]
public static int main(string[] argv)
{
    \u0024Program.init\u0040 = 0;
    int init = \u0024Program.init\u0040;
    ExtraTopLevelOperators
      .PrintFormatLine<FSharpFunc<Program.R, Unit>>(
          (PrintfFormat<FSharpFunc<Program.R, Unit>, TextWriter, Unit, Unit>)
          new PrintfFormat<FSharpFunc<Program.R, Unit>, TextWriter, Unit, Unit, Program.R>("%A")).Invoke(Program.x);
    return 0;
}

public sealed class R : IEquatable<Program.R>, IStructuralEquatable, IComparable<Program.R>, IComparable, IStructuralComparable
{
    internal Program.R r\u0040;
    public Program.R r { get { return this.r\u0040; } }
    public R(Program.R r) { this.r\u0040 = r; }
}

希望它会提示您如何操作.

Hope it will give you a hint how to do that.

这篇关于从C#调用F#`printfn`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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