How do I use System.Net.ConnectStream?

走远了吗. 提交于 2019-12-02 05:20:57

ConnectStream is an internal class, you can't use it explicitly. But it doesn't matter, because you don't need to know that its actual type is ConnectStream: all you need to know is that it's a Stream (the return type declared by GetRequestStream), the actual implementation doesn't really matter.

If you want to specify the type explicitly, just write it like this:

using (Stream postStream = request.GetRequestStream())
{
    postStream.Write(byteData, 0, byteData.Length);
}

(but it has exactly the same meaning as using var)

Theres a great snippet from the var keyword on the InfoQ site. This talks about when and when not to use var. Its not quite as clear cut as don't' use it unless your using linq, its more you use it when you don't need to draw attention to the data type and use typed objects when you need to draw attention to the data type.

Its one of the personal preference things... but normally the best preference is however your boss/code lead/architect likes their code 'grammar' to look to make it uniform.

Without knowing this actual API, GetRequestStream() looks like a factory method, it most likely returns a base-class of ConnectStream.

So you would have to use:

 using (ConnectStream postStream = (ConnectStream) request.GetRequestStream()) { ... }

Note that you can simply hover your mouse over postStream in the editor to see what the compile-time type is.

Diomos

I reused one answer from here: How do I get the filesize from the Microsoft.SharePoint.Client.File object?

It' reply from 'Freejete' and his method 'ReadToEnd' worked like a charm for me.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!