csharp 流扩展方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 流扩展方法相关的知识,希望对你有一定的参考价值。

public static Stream ToStream(this string str)
{
    byte[] byteArray = Encoding.UTF8.GetBytes(str);
    //byte[] byteArray = Encoding.ASCII.GetBytes(str);
    return new MemoryStream(byteArray);
}
public static string ToString(this Stream stream)
{
    var reader = new StreamReader(stream);
    return reader.ReadToEnd();
}
/// <summary>
/// Copy from one stream to another.
/// Example:
/// using(var stream = response.GetResponseStream())
/// using(var ms = new MemoryStream())
/// {
///     stream.CopyTo(ms);
///      // Do something with copied data
/// }
/// </summary>
/// <param name="fromStream">From stream.</param>
/// <param name="toStream">To stream.</param>
public static void CopyTo(this Stream fromStream, Stream toStream)
{
    if (fromStream == null)
        throw new ArgumentNullException("fromStream");
    if (toStream == null)
        throw new ArgumentNullException("toStream");
    var bytes = new byte[8092];
    int dataRead;
    while ((dataRead = fromStream.Read(bytes, 0, bytes.Length)) > 0)
        toStream.Write(bytes, 0, dataRead);
}

以上是关于csharp 流扩展方法的主要内容,如果未能解决你的问题,请参考以下文章

csharp 扩展方法用法示例

csharp JSON.NET:JObject扩展方法

csharp 数组扩展方法

csharp 扩展方法

csharp 扩展方法

csharp 扩展方法