本文整理汇总了C#中this.CopyTo方法的典型用法代码示例。如果您正苦于以下问题:C# this.CopyTo方法的具体用法?C# this.CopyTo怎么用?C# this.CopyTo使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类this
的用法示例。
在下文中一共展示了this.CopyTo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ToByteArray
/// <summary>
/// Returns the content of a stream as byte array.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="bufferSize">The custom buffer size to use.</param>
/// <returns><paramref name="stream" /> as byte array.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="stream" /> is <see langword="null" />.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="bufferSize" /> is invalid.
/// </exception>
public static byte[] ToByteArray(this Stream stream, int? bufferSize = null)
{
if (stream == null)
{
throw new ArgumentNullException("stream");
}
if (bufferSize < 1)
{
throw new ArgumentOutOfRangeException("stream", bufferSize, "Must be 1 at least!");
}
if (stream is MemoryStream)
{
return ((MemoryStream)stream).ToArray();
}
using (var temp = new MemoryStream())
{
if (bufferSize.HasValue)
{
stream.CopyTo(temp, bufferSize.Value);
}
else
{
stream.CopyTo(temp);
}
return temp.ToArray();
}
}
示例2: ShouldBeCopiedOK
internal static void ShouldBeCopiedOK(this IPersistentBufferProvider @this,string iPath)
{
bool res = @this.CopyTo(iPath);
res.Should().BeTrue();
File.Exists(iPath).Should().BeTrue();
IPersistentBufferProvider bc = InternalBufferFactory.GetBufferProviderFromFile(iPath);
bc.Should().NotBeNull();
@this.Compare(bc).Should().BeTrue();
@this.CopyTo("").Should().BeFalse();
}
示例3: BytesToInt
/// <summary>
/// 数组转int,
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static Int32 BytesToInt(this Byte[] bytes)
{
byte[] intBytes = new byte[4];
bytes.CopyTo(intBytes, intBytes.Length - bytes.Length);
if (BitConverter.IsLittleEndian) Array.Reverse(intBytes);
return BitConverter.ToInt32(intBytes, 0);
}
示例4: UnpackToDirectory
public static void UnpackToDirectory(this ZipInputStream zip, string path, IFileSystem fs)
{
ZipEntry theEntry;
while ((theEntry = zip.GetNextEntry()) != null)
{
string directoryName = fs.Path.GetDirectoryName(theEntry.Name);
string fileName = fs.Path.GetFileName(theEntry.Name);
//Create directory for this entry
if (!string.IsNullOrEmpty(directoryName))
fs.Directory.CreateDirectory(fs.Path.Combine(path, directoryName));
//If there is no filename this was a directory entry, skip to the next entry
if (fileName == String.Empty)
continue;
//Copy the file from zip into filesystem
using (Stream streamWriter = fs.File.Create(fs.Path.Combine(path, theEntry.Name)))
{
zip.CopyTo(streamWriter);
streamWriter.Flush();
}
}
Thread.Sleep(100);
}
示例5: GetMemoryStream
public static MemoryStream GetMemoryStream(this Stream stream, int index)
{
MemoryStream retVal = null;
if (stream != null)
{
MemoryStream wrkStream = null;
try
{
wrkStream = new MemoryStream();
stream.CopyTo(wrkStream, index, stream.Length - index);
retVal = wrkStream;
retVal.Position = 0;
wrkStream = null;
}
finally
{
if (wrkStream != null)
{
wrkStream.Dispose();
}
}
}
return retVal;
}
示例6: Clone
/// <summary>
/// Creates a clone and performs a shallow copy of properties from the source object to the destination
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <returns></returns>
public static object Clone(this object source, List<String> ExcludeProperties)
{
Type t = source.GetType();
var clone = Activator.CreateInstance(t);
source.CopyTo(clone, ExcludeProperties);
return clone;
}
示例7: AsMemoryStream
/// <summary>
/// Returns a <see cref="Stream" /> as a <see cref="MemoryStream" />.
/// </summary>
/// <param name="stream">The stream to cast / convert.</param>
/// <param name="moveToBeginning">Move cursor of result stream to beginning or not.</param>
/// <returns>The result stream.</returns>
/// <remarks>
/// <see langword="null" /> is returned if <paramref name="stream" /> is also <see langword="null" />.
/// </remarks>
/// <remarks>
/// If <paramref name="stream" /> is already a memory stream it is simply casted.
/// </remarks>
public static MemoryStream AsMemoryStream(this Stream stream, bool moveToBeginning = true)
{
var result = stream as MemoryStream;
if (result == null && stream != null)
{
result = new MemoryStream();
try
{
stream.CopyTo(result);
}
catch
{
result.Dispose();
throw;
}
}
if (result != null)
{
if (moveToBeginning)
{
result.Position = 0;
}
}
return result;
}
示例8: GetString
public static String GetString(this IList<char> chars)
{
char[] array = new char[chars.Count];
chars.CopyTo(array, 0);
chars.Clear();
return new String(array);
}
示例9: ToBytes
public static byte[] ToBytes(this BitArray array)
{
var messageBytes = new byte[array.Count];
array.CopyTo(messageBytes, 0);
return messageBytes;
}
示例10: AsRandomAccessStream
public static IRandomAccessStream AsRandomAccessStream( this Stream stream )
{
var randomAccessStream = new InMemoryRandomAccessStream();
var s = randomAccessStream.AsStreamForWrite();
stream.CopyTo(s);
return randomAccessStream;
}
示例11: WriteToFile
public static void WriteToFile(this Stream stream, string filePath)
{
using (var sw = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
stream.CopyTo(sw);
}
}
示例12: ToInteger
public static int ToInteger(this BitArray bits)
{
var number = new int[1];
bits.CopyTo(number, 0);
var numberValue = number[0];
return numberValue;
}
示例13: AsBytes
public static byte[] AsBytes(this Stream stream)
{
using (MemoryStream ms = new MemoryStream ()) {
stream.CopyTo (ms);
return ms.ToArray ();
}
}
示例14: CopyStream
/// <summary>
/// Copies the stream.
/// </summary>
/// <param name="sourceStream">The source stream.</param>
/// <param name="destinationStream">The destination stream.</param>
public static void CopyStream(this Stream sourceStream, Stream destinationStream)
{
if (sourceStream != null && destinationStream != null)
{
sourceStream.CopyTo(destinationStream);
}
}
示例15: Append
/// <summary>
/// 将一个BitArray拼接在当前对象之后
/// </summary>
/// <param name="current"></param>
/// <param name="after"></param>
/// <returns></returns>
public static BitArray Append(this BitArray current, BitArray after)
{
var bools = new bool[current.Length + after.Length];
current.CopyTo(bools, 0);
after.CopyTo(bools, current.Length);
return new BitArray(bools);
}