Windows Azure Cloud Storage ermöglicht es Ihnen bereits ab 0,10€ pro GB/Monat die Vorteile der Cloud zu nutzen.
Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1550 | Anzahl registrierter User: 1841 | Besucher online: 29
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

Zum De/Serialisieren von Objekten


Autor: Vertexwahn
Sprache: C#
Bewertung: 7,4
(1 Bewertung)
Anzahl der Aufrufe: 9929
  
Kick it on dotnet-kicks.de  

Beschreibung:

Zum De/Serialisieren von Objekten

Abgelegt unter: Serialization, MemoryStream, BinaryWriter, Serialisieren, Objekt.



C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Vertexwahn
{
    public class Serializer
    {
        public static byte[] SerializeIt(object obj)
        {
            try
            {
                MemoryStream bout = new MemoryStream();
                BinaryWriter objOut = new BinaryWriter(bout);
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(objOut.BaseStream, obj);
                objOut.Close();
                return bout.ToArray();
            }
            catch (IOException e)
            {
                throw new ApplicationException("Serialization failed: " + e.Message);
            }
        }

        public static object DeserializeIt(byte[] data)
        {
            if (data == null)
                return null;
            try
            {
                MemoryStream bin = new MemoryStream(data);
                BinaryFormatter formatter = new BinaryFormatter();
                object obj = formatter.Deserialize(bin);
                bin.Close();
                return obj;
            }
            catch (IOException e)
            {
                throw new ApplicationException("Deserialization failed: " +
                                               e.Message);
            }
            catch (Exception e)
            {
                throw new ApplicationException("Class not found: " + e.Message);
            }
        }
    }
}
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)



schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.