简体   繁体   English

用F#编写此AWS C#代码段

[英]Writing this AWS C# code snippet in F#

I'm new to F# and was just wondering how to write the below C# code in F#? 我是F#的新手,只是想知道如何在F#中编写以下C#代码?

// Issue request and remember to dispose of the response
using (GetObjectResponse response = client.GetObject(request))
{
    using (StreamReader reader = new StreamReader(response.ResponseStream))
    {
        string contents = reader.ReadToEnd();
        Console.WriteLine("Object - " + response.Key);
        Console.WriteLine(" Version Id - " + response.VersionId);
        Console.WriteLine(" Contents - " + contents);
    }
}

I have read up on the documentation using use and came up with this: 我已经阅读了有关use的文档,并提出了以下建议:

    use response = s3Client.GetObject(req)
    (
      use reader = new StreamReader(response.ResponseStream)
      urlCheck = reader.ReadToEnd())
      Console.WriteLine(urlCheck)

but it's not working at all. 但它根本不起作用。 Could anybody help? 有人可以帮忙吗?

EDIT 编辑
I used this link: f# keyword use and using as a template for my solution above but it didn't work. 我使用了以下链接: f#关键字的用法,并用作上述解决方案的模板,但是没有用。

The error I get is that "reader is not a function and cannot be applied". 我得到的错误是“阅读器不是函数,无法应用”。

Also, I know I could just leave it in C#, but I'd like to see if I can port it over to F#. 另外,我知道我可以将其保留在C#中,但是我想看看是否可以将其移植到F#中。 Any advice on this would be much appreciated. 任何对此的建议将不胜感激。

The use keyword in F# has slightly different mechanics than the C#'s using . F#中的use关键字的机制与C#的using略有不同。 One major difference is that in C# you explicitly specify the scope of the using with curly braces, but the F#'s use affects the whole "current block" ( let -binding or member), from the use to the end. 一个主要的区别是,在C#中,您using花括号显式指定了使用范围,但是F#的use会影响整个“当前块”( let -binding或member),从use到结束。 So that you don't have to explicitly "nest" (ie indent) the code that is "under" the use . 这样您就不必显式“嵌套”(即缩进) use下的代码。 Just keep on writing as usual: 像往常一样继续写作:

use response = s3Client.GetObject(req)
use reader = new StreamReader(response.ResponseStream)
let urlCheck = reader.ReadToEnd()
Console.WriteLine(urlCheck)

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

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