[英]How do I use System.Net.ConnectStream?
I am trying to get my head around some of my predecessors code who, helpfully, has used 'var' to declare everything. 我试图让我的一些前任代码有所帮助,这些代码有用地使用了'var'来声明所有内容。
I have a using statement which is below: 我有一个using语句,如下所示:
using (var postStream = request.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
}
When I put a breakpoint here, postStream shows up in the Autos window as System.Net.ConnectStream. 当我在此处放置一个断点时,postStream在Autos窗口中显示为System.Net.ConnectStream。 Instead of 'var' I want to use 'ConnectStream' but the compiler doesn't like this. 我想使用“ ConnectStream”代替“ var”,但是编译器不喜欢这样。
What am I missing, why can't I write my code like this: 我缺少什么,为什么不能这样编写代码:
using (ConnectStream postStream = request.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
}
I know this is trivial but I was always taught not to use 'var' unless you have a specific reason to do so (such as when dealing with LINQ). 我知道这很简单,但是除非您有特定的理由(例如在处理LINQ时),否则我总是被教导不要使用'var'。 Am I wrong? 我错了吗?
ConnectStream
is an internal class, you can't use it explicitly. ConnectStream
是一个内部类,您不能显式使用它。 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. 但这没关系,因为您不需要知道它的实际类型是ConnectStream
:您只需要知道它是一个Stream
(由GetRequestStream
声明的返回类型),实际的实现并不重要。
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
) (但它的含义与使用var
完全相同)
Theres a great snippet from the var keyword on the InfoQ site. InfoQ网站上的var关键字有一个很棒的片段。 This talks about when and when not to use var. 讨论何时以及何时不使用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. 除非您使用linq,否则它不像不使用它那样清晰明了;在不需要引起注意数据类型的情况下,使用更多,而在需要引起关注的数据类型时,则使用类型化的对象。
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. 不知道这个实际的API, GetRequestStream()
看起来像一个工厂方法,它很可能返回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. 请注意,您只需将鼠标悬停在编辑器中的postStream
上,即可查看编译时类型。
I reused one answer from here: How do I get the filesize from the Microsoft.SharePoint.Client.File object? 我从这里重用了一个答案: 如何从Microsoft.SharePoint.Client.File对象获取文件大小?
It' reply from 'Freejete' and his method 'ReadToEnd' worked like a charm for me. 来自“ Freejete”的回复和他的方法“ ReadToEnd”对我来说就像是一种魅力。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.