Folge dem Video um zu sehen, wie unsere Website als Web-App auf dem Startbildschirm installiert werden kann.
Anmerkung: Diese Funktion ist in einigen Browsern möglicherweise nicht verfügbar.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace De.Tutorials.Training
{
public class ObjectToByteArrayExample
{
public static void Main(string[] args)
{
KeyValue<string, string> keyValue = new KeyValue<string, string>("Foo","Bar");
Console.WriteLine(keyValue + " " + keyValue.GetHashCode());
byte[] data = ToByteArray(keyValue);
object o = ToObject(data);
keyValue = (KeyValue<string, string>)o;
Console.WriteLine(keyValue + " " + keyValue.GetHashCode());
}
private static object ToObject(byte[] data)
{
MemoryStream memoryStream = new MemoryStream(data);
BinaryFormatter binaryFormatter = new BinaryFormatter();
memoryStream.Position = 0;
return binaryFormatter.Deserialize(memoryStream);
}
private static byte[] ToByteArray(Object o)
{
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, o);
memoryStream.Position = 0;
return memoryStream.GetBuffer();
}
}
[Serializable]
class KeyValue<TKey,TValue>
{
public TValue Value
{
get { return @value; }
set { @value = value; }
}private TValue @value;
public TKey Key
{
get { return key; }
set { key = value; }
} private TKey key;
public KeyValue(TKey key, TValue value)
{
this.key = key;
this.value = value;
}
public override string ToString()
{
return Key +": " + Value;
}
}
}