これがSilverlightで機能するかどうかはわかりませんが、コンソールアプリケーションでは機能し、安全でないコードは必要ありません。
double値をバイト配列に入れることができる場合は、バイト配列のバイトを交換してエンディアンを変更できます。このプロセスを逆にして、バイト配列をdoubleに戻すこともできます。
次のコードは、System.InteropServicesを使用してdouble配列とbyte配列を変換する方法を示しています。Mainメソッドは、コンソールに8と3.14159の2つの値を返します。8は、doubleから8バイトのバイト配列が正常に作成されたことを示し、3.14159は、doubleがバイト配列から正しく抽出されたことを示します。
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
double d = 3.14159d;
byte[] b = ToByteArray(d);
Console.WriteLine(b.Length);
Console.ReadLine();
double n = FrpmByteArray(b);
Console.WriteLine(n.ToString());
Console.ReadLine();
}
public static byte[] ToByteArray(object anything)
{
int structsize = Marshal.SizeOf(anything);
IntPtr buffer = Marshal.AllocHGlobal(structsize);
Marshal.StructureToPtr(anything, buffer, false);
byte[] streamdatas = new byte[structsize];
Marshal.Copy(buffer, streamdatas, 0, structsize);
Marshal.FreeHGlobal(buffer);
return streamdatas;
}
public static double FromByteArray(byte[] b)
{
GCHandle handle = GCHandle.Alloc(b, GCHandleType.Pinned);
double d = (double)Marshal.PtrToStructure(
handle.AddrOfPinnedObject(),
typeof(double));
handle.Free();
return d;
}
}
}