CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
MobileDictionary.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="MobileDictionary.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Defines a dictionary that can be serialized through</summary>
7//-----------------------------------------------------------------------
8using System.Collections.Generic;
9using System.Linq;
11using System;
12using System.Reflection;
13using Csla.Reflection;
14
15namespace Csla.Core
16{
23 [Serializable()]
24 public class MobileDictionary<K, V> : Dictionary<K, V>, IMobileObject
25 {
26 private bool _keyIsMobile;
27 private bool _valueIsMobile;
28
33 {
34 DetermineTypes();
35 }
36
44 public MobileDictionary(int capacity)
45 : base(capacity)
46 {
47 DetermineTypes();
48 }
49
57 public MobileDictionary(IEqualityComparer<K> comparer)
58 : base(comparer)
59 {
60 DetermineTypes();
61 }
62
69 public MobileDictionary(Dictionary<K, V> dict)
70 : base(dict)
71 {
72 DetermineTypes();
73 }
74
81 public bool Contains(K key)
82 {
83 return base.ContainsKey(key);
84 }
85
86#if !(ANDROID || IOS) && !NETFX_CORE
92 protected MobileDictionary(System.Runtime.Serialization.SerializationInfo info,
93 System.Runtime.Serialization.StreamingContext context)
94 : base(info, context)
95 {
96 DetermineTypes();
97 }
98#endif
99
100 private void DetermineTypes()
101 {
102 _keyIsMobile = typeof(Csla.Serialization.Mobile.IMobileObject).IsAssignableFrom(typeof(K));
103 _valueIsMobile = typeof(Csla.Serialization.Mobile.IMobileObject).IsAssignableFrom(typeof(V));
104 }
105
106 #region IMobileObject Members
107
108 private const string _keyPrefix = "k";
109 private const string _valuePrefix = "v";
110
112 {
113 info.AddValue("count", this.Keys.Count);
114 }
115
117 {
118 int count = 0;
119 foreach (var key in this.Keys)
120 {
121 if (_keyIsMobile)
122 {
123 SerializationInfo si = formatter.SerializeObject(key);
124 info.AddChild(_keyPrefix + count, si.ReferenceId);
125 }
126 else
127 {
128 info.AddValue(_keyPrefix + count, key);
129 }
130
131 if (_valueIsMobile)
132 {
133 SerializationInfo si = formatter.SerializeObject(this[key]);
134 info.AddChild(_valuePrefix + count, si.ReferenceId);
135 }
136 else
137 {
138 V value = this[key];
139 info.AddValue(_valuePrefix + count, value);
140 }
141 count++;
142 }
143 }
144
146 { }
147
149 {
150 int count = info.GetValue<int>("count");
151
152 for (int index = 0; index < count; index++)
153 {
154 K key;
155 if (_keyIsMobile)
156 key = (K)formatter.GetObject(info.Children[_keyPrefix + index].ReferenceId);
157 else
158 key = info.GetValue<K>(_keyPrefix + index);
159
160 V value;
161 if (_valueIsMobile)
162 value = (V)formatter.GetObject(info.Children[_valuePrefix + index].ReferenceId);
163 else
164 value = info.GetValue<V>(_valuePrefix + index);
165
166 Add(key, value);
167 }
168 }
169
170 #endregion
171 }
172}
Defines a dictionary that can be serialized through the SerializationFormatterFactory....
MobileDictionary(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
Creates an instance of the object for serialization.
MobileDictionary(Dictionary< K, V > dict)
Creates an instance of the object based on the supplied dictionary, whose elements are copied to the ...
MobileDictionary(int capacity)
Creates an instance of the object based on the supplied dictionary, whose elements are copied to the ...
MobileDictionary(IEqualityComparer< K > comparer)
Creates an instance of the object based on the supplied dictionary, whose elements are copied to the ...
bool Contains(K key)
Gets a value indicating whether the dictionary contains the specified key value.
MobileDictionary()
Creates an instance of the object.
Serializes and deserializes objects at the field level.
IMobileObject GetObject(int referenceId)
Gets a deserialized object based on the object's reference id within the serialization stream.
SerializationInfo SerializeObject(object obj)
Serializes an object into a SerializationInfo object.
Object containing the serialization data for a specific object.
int ReferenceId
Reference number for this object.
Dictionary< string, ChildData > Children
Dictionary containing child reference data.
void AddChild(string name, int referenceId)
Adds a child to the list of child references.
void AddValue(string name, object value)
Adds a value to the serialization stream.
Interface to be implemented by any object that supports serialization by the SerializationFormatterFa...
void GetChildren(SerializationInfo info, MobileFormatter formatter)
Method called by MobileFormatter when an object should serialize its child references.
void GetState(SerializationInfo info)
Method called by MobileFormatter when an object should serialize its data.
void SetChildren(SerializationInfo info, MobileFormatter formatter)
Method called by MobileFormatter when an object should deserialize its child references.
void SetState(SerializationInfo info)
Method called by MobileFormatter when an object should be deserialized.
@ Serializable
Prevents updating or inserting until the transaction is complete.