CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
CslaBinaryReader.cs
Go to the documentation of this file.
1using Csla.Properties;
2using System;
3using System.Collections.Generic;
4using System.IO;
5
7{
13 {
14 private readonly Dictionary<int, string> keywordsDictionary;
15 private ApplicationContext ApplicationContext { get; set; }
16
21 public CslaBinaryReader(ApplicationContext applicationContext)
22 {
23 ApplicationContext = applicationContext;
24 keywordsDictionary = new Dictionary<int, string>();
25 }
26
33 public List<SerializationInfo> Read(Stream serializationStream)
34 {
35 var returnValue = new List<SerializationInfo>();
36 int childCount, valueCount, referenceId;
37 string systemName, enumTypeName;
38 bool isDirty;
39 object value;
40 this.keywordsDictionary.Clear();
41
42 using (var reader = new BinaryReader(serializationStream))
43 {
44 var totalCount = reader.ReadInt32();
45 for (var counter = 0; counter < totalCount; counter++)
46 {
47 var info = new SerializationInfo()
48 {
49 ReferenceId = reader.ReadInt32(),
50 TypeName = ReadString(reader)
51 };
52
53 childCount = reader.ReadInt32();
54 for (var childCounter = 0; childCounter < childCount; childCounter++)
55 {
56 systemName = ReadString(reader);
57 isDirty = reader.ReadBoolean();
58 referenceId = reader.ReadInt32();
59 info.AddChild(systemName, referenceId, isDirty);
60 }
61
62 valueCount = reader.ReadInt32();
63 for (var valueCounter = 0; valueCounter < valueCount; valueCounter++)
64 {
65 systemName = ReadString(reader);
66 enumTypeName = ReadString(reader);
67 isDirty = reader.ReadBoolean();
68 value = ReadObject(reader);
69 info.AddValue(systemName, value, isDirty, string.IsNullOrEmpty(enumTypeName) ? null : enumTypeName);
70 }
71 returnValue.Add(info);
72 }
73 }
74
75 return returnValue;
76 }
77
78 private string ReadString(BinaryReader reader) =>
79 this.ReadString(reader, (CslaKnownTypes)reader.ReadByte());
80
81 private string ReadString(BinaryReader reader, CslaKnownTypes knownType)
82 {
83 switch (knownType)
84 {
85 case CslaKnownTypes.String:
86 return reader.ReadString();
87 case CslaKnownTypes.StringWithDictionaryKey:
88 var systemString = reader.ReadString();
89 this.keywordsDictionary.Add(reader.ReadInt32(), systemString);
90 return systemString;
91 case CslaKnownTypes.StringDictionaryKey:
92 return this.keywordsDictionary[reader.ReadInt32()];
93 default:
94 throw new ArgumentOutOfRangeException(Resources.UnandledKNownTypeException);
95 }
96 }
97
98 private object ReadObject(BinaryReader reader)
99 {
100 var knownType = (CslaKnownTypes)reader.ReadByte();
101 switch (knownType)
102 {
103 case CslaKnownTypes.IMobileObject:
104 using (MemoryStream arrayBuffer = new MemoryStream(reader.ReadBytes(reader.ReadInt32())))
105 {
106 var formatter = SerializationFormatterFactory.GetFormatter(ApplicationContext);
107 var obj = formatter.Deserialize(arrayBuffer);
108 return obj;
109 }
110 case CslaKnownTypes.Boolean:
111 return reader.ReadBoolean();
112 case CslaKnownTypes.Char:
113 return reader.ReadChar();
114 case CslaKnownTypes.SByte:
115 return reader.ReadSByte();
116 case CslaKnownTypes.Byte:
117 return reader.ReadByte();
118 case CslaKnownTypes.Int16:
119 return reader.ReadInt16();
120 case CslaKnownTypes.UInt16:
121 return reader.ReadUInt16();
122 case CslaKnownTypes.Int32:
123 return reader.ReadInt32();
124 case CslaKnownTypes.UInt32:
125 return reader.ReadUInt32();
126 case CslaKnownTypes.Int64:
127 return reader.ReadInt64();
128 case CslaKnownTypes.UInt64:
129 return reader.ReadUInt64();
130 case CslaKnownTypes.Single:
131 return reader.ReadSingle();
132 case CslaKnownTypes.Double:
133 return reader.ReadDouble();
134 case CslaKnownTypes.Decimal:
135 var decimalBits = new int[4];
136 for (var counter = 0; counter < 4; counter++)
137 {
138 decimalBits[counter] = reader.ReadInt32();
139 }
140 return new decimal(decimalBits);
141 case CslaKnownTypes.DateTime:
142 return new DateTime(reader.ReadInt64());
143 case CslaKnownTypes.TimeSpan:
144 return new TimeSpan(reader.ReadInt64());
145 case CslaKnownTypes.DateTimeOffset:
146 return new DateTimeOffset(reader.ReadInt64(), new TimeSpan(reader.ReadInt64()));
147 case CslaKnownTypes.Guid:
148 return new Guid(reader.ReadBytes(16)); // 16 bytes in a Guid
149 case CslaKnownTypes.ByteArray:
150 return reader.ReadBytes(reader.ReadInt32());
151 case CslaKnownTypes.ByteArrayArray:
152 var count = reader.ReadInt32();
153 var result = new byte[count][];
154 for (int i = 0; i < count; i++)
155 result[i] = reader.ReadBytes(reader.ReadInt32());
156 return result;
157 case CslaKnownTypes.CharArray:
158 return reader.ReadChars(reader.ReadInt32());
159 case CslaKnownTypes.ListOfInt:
160 var total = reader.ReadInt32();
161 var buffer = new int[total];
162 for (var counter = 0; counter < total; counter++)
163 {
164 buffer[counter] = reader.ReadInt32();
165 }
166 return new List<int>(buffer);
167 case CslaKnownTypes.Null:
168 return null;
169 case CslaKnownTypes.String:
170 case CslaKnownTypes.StringWithDictionaryKey:
171 case CslaKnownTypes.StringDictionaryKey:
172 return ReadString(reader, knownType);
173 default:
174 throw new ArgumentOutOfRangeException(Resources.UnandledKNownTypeException);
175 }
176 }
177 }
178}
Provides consistent context information between the client and server DataPortal objects.
A strongly-typed resource class, for looking up localized strings, etc.
static string UnandledKNownTypeException
Looks up a localized string similar to Unhandled CSLA Known type was found.
This is a class that is responsible for deserializing SerializationInfo objects for receiving the dat...
CslaBinaryReader(ApplicationContext applicationContext)
Creates new instance of CslaBinaryReader
List< SerializationInfo > Read(Stream serializationStream)
Read a data from a stream, typically MemoryStream, and convert it into a list of SerializationInfo ob...
Object containing the serialization data for a specific object.
Represents a reader class that can be used to read the data sent across the wire in byte array format...
Definition: ICslaReader.cs:12
CslaKnownTypes
This enumeration contains the list of known types that CslaBinaryReader and CslaBinaryWriterknow abou...
@ DateTimeOffset
Date/time plus time zone / DateTimeOffset
@ Guid
Globally unique identifier / Guid