Autor Beitrag
Raven280438
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 99



BeitragVerfasst: So 24.10.10 20:44 
Hi,

ich hab in einem Programm eine Klasse serialisiert und in einer Datei gespeichert.

Jetzt will ich in einem anderen Programm die Klasse wieder deserialisieren. Die Klasse liegt in beiden Programmen in einer DLL vor, ist also überall exakt gleich.

Der Code zum Deserialisieren lautet wie folgt:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
try
{
    FileInfo fi = new FileInfo(filename);
    if (!this.texturen.ContainsKey(fi.Name)) {
        CSTTexture tex = new CSTTexture();
        BinaryFormatter binF = new BinaryFormatter();
        FileStream fs = new FileStream(filename, FileMode.Open);

        tex = (CSTTexture)binF.Deserialize(fs);

        this.texturen.Add(fi.Name, tex);
        fs.Flush();
        fs.Close();
    }

    return true;
}
catch (Exception e)
{
    MessageBox.Show(e.ToString(), "fehler");
    return false;
}


Aber dabei bekomm ich folgende Exception:
user defined image


Wie kann ich in dem Anderen Programm die Klasse deserialisieren?


Gruß
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 24.10.10 20:58 
Ich kenne mich da jetzt nicht ganz so gut aus, aber ich vermute du musst einen eigenen Binder implementieren.

Mehr dazu steht hier:
blog.norberteder.com...y=entry070712-205835
Christoph1972
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 690
Erhaltene Danke: 16


VS2015 Pro / C# & VB.Net
BeitragVerfasst: So 24.10.10 21:09 
Hi,

in der serialisierten Datei wird scheinbar der Name des Assemblies abgelegt. Ich habe gestern den Namen meines akktuellen Projekts verändert und hatte dann die gleiche Fehlermeldung. Der Link von jaenicke sollte aber abhilfe schaffen.

_________________
Gruß
Christoph
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: So 24.10.10 21:57 
user profile iconRaven280438 hat folgendes geschrieben Zum zitierten Posting springen:
Die Klasse liegt in beiden Programmen in einer DLL vor
Wenn das die "ST-Texturer"-Assembly ist, deckt sich das aber nicht wirklich mit der Fehlermeldung, nach der sie eben nicht vorliegt, oder?

Grundsätzlich muss aber jedem (@Christian ;) ) klar sein, dass der BinaryFormatter eigentlich nur für Remoting gedacht ist, keine Versionierung kennt und bei der kleinsten Änderung zusammenbricht. Es gibt andere binäre Serialisierer wie protobuf-net, die das Datei-Format ordentlich spezifizieren und nicht dem Zufall überlassen.

_________________
>λ=
Raven280438 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 99



BeitragVerfasst: Mo 25.10.10 15:25 
Hi,

danke für die Antworten.

Wie genau funktioniert das mit dem Binder? Was muss ich machen, damit das bei meinem Programm auch läuft?


Gruß
Raven280438 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 99



BeitragVerfasst: Mo 25.10.10 15:46 
Nachtrag:

Oh den Post von Kha hab ich ganz übersehn. Ich werd mir protobuf-net mal ansehn ;)
Raven280438 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 99



BeitragVerfasst: Mo 25.10.10 20:18 
Hi,

habs getestet und es funktioniert. ;)


Meine Frage ist aber, was passiert, wenn ich an der Klasse die serialisiert wird irgendwas ändere? Wie bekomm ich das hin, dass ich die trotzdem noch einlesen kann und die fehlenden Attribute mit Standard-Werten fülle?



Gruß
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Mo 25.10.10 20:26 
Redest du jetzt von Protobuf oder vom BinaryFormatter aus dem Framework? Wenn es um letzteres geht siehe hier.
Raven280438 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 99



BeitragVerfasst: Mo 25.10.10 20:29 
Hi,

ne ich hab jetzt Protobuf eingesetzt, weil ich das ja in verschiedenen Programmen nutzen will.
Raven280438 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 99



BeitragVerfasst: Di 26.10.10 15:59 
Hi,

ich will jetzt auch mal das mit dem BinaryFormatter versuchen.
Kann mir jemand erklären, wie genau das mit dem Binder funktioniert?
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Di 26.10.10 16:29 
Ist in der Hilfe zum SerializationBinder eigentlich gut mit Beispiel beschrieben. Wenn die Hilfe nicht hilft solltest du dann nochmal konkreter fragen.