- Регистрация
- 21 Фев 2017
- Сообщения
- 219
- Реакции
- 74
- Баллы
- 3
[HIDE]
[/HIDE]
Код:
public static class GetBytes
{
public static byte[] GetBytes(this bool obj)
{
var b = new byte[1];
b[0] = (obj ? (byte)1 : (byte)0);
return b;
}
public static byte[] GetBytes(this bool[] obj)
{
var b = new byte[0];
return obj.Aggregate(b, (Current, bl) => Current.Add(bl.GetBytes()));
}
[SecuritySafeCritical]
public static unsafe byte[] GetBytes(this short obj)
{
var bytes = new byte[2];
fixed (byte* b = bytes)
*((short*)b) = obj;
return bytes;
}
public static byte[] GetBytes(this short[] obj)
{
var result = new byte[obj.Length * Constants.SizeOfShort];
Buffer.BlockCopy(obj, 0, result, 0, result.Length);
return result;
}
public static byte[] GetBytes(this ushort obj)
{
return GetBytes((short)obj);
}
public static byte[] GetBytes(this ushort[] obj)
{
var result = new byte[obj.Length * Constants.SizeOfUShort];
Buffer.BlockCopy(obj, 0, result, 0, result.Length);
return result;
}
public static byte[] GetBytes(this char obj)
{
return GetBytes((short)obj);
}
public static byte[] GetBytes(this char[] obj)
{
var result = new byte[obj.Length * Constants.SizeOfChar];
Buffer.BlockCopy(obj, 0, result, 0, result.Length);
return result;
}
[SecuritySafeCritical]
public static unsafe byte[] GetBytes(this int obj)
{
var bytes = new byte[4];
fixed (byte* b = bytes)
*((int*)b) = obj;
return bytes;
}
public static byte[] GetBytes(this int[] obj)
{
var result = new byte[obj.Length * Constants.SizeOfInt];
Buffer.BlockCopy(obj, 0, result, 0, result.Length);
return result;
}
public static byte[] GetBytes(this uint obj)
{
return GetBytes((int)obj);
}
public static byte[] GetBytes(this uint[] obj)
{
var result = new byte[obj.Length * Constants.SizeOfUInt];
Buffer.BlockCopy(obj, 0, result, 0, result.Length);
return result;
}
public static unsafe byte[] GetBytes(this long obj)
{
var bytes = new byte[8];
fixed (byte* b = bytes)
*((long*)b) = obj;
return bytes;
}
public static byte[] GetBytes(this long[] obj)
{
var result = new byte[obj.Length * Constants.SizeOfLong];
Buffer.BlockCopy(obj, 0, result, 0, result.Length);
return result;
}
public static byte[] GetBytes(this ulong obj)
{
return GetBytes((long)obj);
}
public static byte[] GetBytes(this ulong[] obj)
{
var result = new byte[obj.Length * Constants.SizeOfULong];
Buffer.BlockCopy(obj, 0, result, 0, result.Length);
return result;
}
[SecuritySafeCritical]
public static unsafe byte[] GetBytes(this float obj)
{
return GetBytes(*(int*)&obj);
}
public static byte[] GetBytes(this float[] obj)
{
var result = new byte[obj.Length * Constants.SizeOfFloat];
Buffer.BlockCopy(obj, 0, result, 0, result.Length);
return result;
}
[SecuritySafeCritical]
public static unsafe byte[] GetBytes(this double obj)
{
return GetBytes(*(long*)&obj);
}
public static byte[] GetBytes(this double[] obj)
{
var result = new byte[obj.Length * Constants.SizeOfDouble];
Buffer.BlockCopy(obj, 0, result, 0, result.Length);
return result;
}
public static byte[] GetBytes(this string obj)
{
var bytes = new byte[obj.Length * Constants.SizeOfChar];
Buffer.BlockCopy(obj.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
public static byte[] GetBytes(this string[] obj)
{
var len = obj.Where(ss => ss != null).Sum(ss => ss.Length);
var bytes = new byte[len * Constants.SizeOfChar];
var ptr = 0;
foreach (var ss in obj)
{
if (ss == null) continue;
Buffer.BlockCopy(ss.ToCharArray(), 0, bytes, ptr, ss.Length);
ptr += ss.Length * Constants.SizeOfChar;
}
return bytes;
}
public static byte[] GetBytes(this SecureString obj)
{
return new[] {obj}.GetBytes();
}
public static byte[] GetBytes(this SecureString[] obj)
{
var result = new List<byte[]>();
foreach (var ss in obj)
{
if (ss == null) continue;
using (var wrapper = new SecureStringHelper(ss))
{
var securebytes = wrapper.ToByteArray();
result.Add(securebytes.CloneTo());
}
}
var tl = result.Sum(ba => ba.Length);
var r = new byte[tl];
return result.Aggregate(r, (Current, ba) => Current.Add(ba));
}
}