mscorlib(4.0.0.0) API with additions
SettingsPropertyValue.cs
3 using System.IO;
4 using System.Reflection;
8 
9 namespace System.Configuration
10 {
12  public class SettingsPropertyValue
13  {
14  private object _Value;
15 
16  private object _SerializedValue;
17 
18  private bool _Deserialized;
19 
20  private bool _IsDirty;
21 
22  private SettingsProperty _Property;
23 
24  private bool _ChangedSinceLastSerialized;
25 
26  private bool _UsingDefaultValue = true;
27 
30  public string Name => _Property.Name;
31 
35  public bool IsDirty
36  {
37  get
38  {
39  return _IsDirty;
40  }
41  set
42  {
43  _IsDirty = value;
44  }
45  }
46 
49  public SettingsProperty Property => _Property;
50 
54  public bool UsingDefaultValue => _UsingDefaultValue;
55 
59  public object PropertyValue
60  {
61  get
62  {
63  if (!_Deserialized)
64  {
65  _Value = Deserialize();
66  _Deserialized = true;
67  }
68  if (_Value != null && !Property.PropertyType.IsPrimitive && !(_Value is string) && !(_Value is DateTime))
69  {
70  _UsingDefaultValue = false;
71  _ChangedSinceLastSerialized = true;
72  _IsDirty = true;
73  }
74  return _Value;
75  }
76  set
77  {
78  _Value = value;
79  _IsDirty = true;
80  _ChangedSinceLastSerialized = true;
81  _Deserialized = true;
82  _UsingDefaultValue = false;
83  }
84  }
85 
89  public object SerializedValue
90  {
91  [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
92  get
93  {
94  if (_ChangedSinceLastSerialized)
95  {
96  _ChangedSinceLastSerialized = false;
97  _SerializedValue = SerializePropertyValue();
98  }
99  return _SerializedValue;
100  }
101  [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
102  set
103  {
104  _UsingDefaultValue = false;
105  _SerializedValue = value;
106  }
107  }
108 
112  public bool Deserialized
113  {
114  get
115  {
116  return _Deserialized;
117  }
118  set
119  {
120  _Deserialized = value;
121  }
122  }
123 
127  {
128  _Property = property;
129  }
130 
131  private bool IsHostedInAspnet()
132  {
133  return AppDomain.CurrentDomain.GetData(".appDomain") != null;
134  }
135 
136  private object Deserialize()
137  {
138  object obj = null;
139  if (SerializedValue != null)
140  {
141  try
142  {
143  if (SerializedValue is string)
144  {
145  obj = GetObjectFromString(Property.PropertyType, Property.SerializeAs, (string)SerializedValue);
146  }
147  else
148  {
149  MemoryStream memoryStream = new MemoryStream((byte[])SerializedValue);
150  try
151  {
152  obj = new BinaryFormatter().Deserialize(memoryStream);
153  }
154  finally
155  {
156  memoryStream.Close();
157  }
158  }
159  }
160  catch (Exception ex)
161  {
162  try
163  {
164  if (IsHostedInAspnet())
165  {
166  object[] args = new object[3]
167  {
168  Property,
169  this,
170  ex
171  };
172  Type type = Type.GetType("System.Web.Management.WebBaseEvent, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", throwOnError: true);
173  type.InvokeMember("RaisePropertyDeserializationWebErrorEvent", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, args, CultureInfo.InvariantCulture);
174  }
175  }
176  catch
177  {
178  }
179  }
180  if (obj != null && !Property.PropertyType.IsAssignableFrom(obj.GetType()))
181  {
182  obj = null;
183  }
184  }
185  if (obj == null)
186  {
187  _UsingDefaultValue = true;
188  if (Property.DefaultValue == null || Property.DefaultValue.ToString() == "[null]")
189  {
191  {
192  return SecurityUtils.SecureCreateInstance(Property.PropertyType);
193  }
194  return null;
195  }
196  if (!(Property.DefaultValue is string))
197  {
198  obj = Property.DefaultValue;
199  }
200  else
201  {
202  try
203  {
204  obj = GetObjectFromString(Property.PropertyType, Property.SerializeAs, (string)Property.DefaultValue);
205  }
206  catch (Exception ex2)
207  {
208  throw new ArgumentException(System.SR.GetString("Could_not_create_from_default_value", Property.Name, ex2.Message));
209  }
210  }
211  if (obj != null && !Property.PropertyType.IsAssignableFrom(obj.GetType()))
212  {
213  throw new ArgumentException(System.SR.GetString("Could_not_create_from_default_value_2", Property.Name));
214  }
215  }
216  if (obj == null)
217  {
218  if (!(Property.PropertyType == typeof(string)))
219  {
220  try
221  {
222  obj = SecurityUtils.SecureCreateInstance(Property.PropertyType);
223  return obj;
224  }
225  catch
226  {
227  return obj;
228  }
229  }
230  obj = "";
231  }
232  return obj;
233  }
234 
235  private static object GetObjectFromString(Type type, SettingsSerializeAs serializeAs, string attValue)
236  {
237  if (type == typeof(string) && (attValue == null || attValue.Length < 1 || serializeAs == SettingsSerializeAs.String))
238  {
239  return attValue;
240  }
241  if (attValue == null || attValue.Length < 1)
242  {
243  return null;
244  }
245  switch (serializeAs)
246  {
247  case SettingsSerializeAs.Binary:
248  {
249  byte[] buffer = Convert.FromBase64String(attValue);
250  MemoryStream memoryStream = null;
251  try
252  {
253  memoryStream = new MemoryStream(buffer);
254  return new BinaryFormatter().Deserialize(memoryStream);
255  }
256  finally
257  {
258  memoryStream?.Close();
259  }
260  }
261  case SettingsSerializeAs.Xml:
262  {
263  StringReader textReader = new StringReader(attValue);
264  XmlSerializer xmlSerializer = new XmlSerializer(type);
265  return xmlSerializer.Deserialize(textReader);
266  }
267  case SettingsSerializeAs.String:
268  {
269  TypeConverter converter = TypeDescriptor.GetConverter(type);
270  if (converter != null && converter.CanConvertTo(typeof(string)) && converter.CanConvertFrom(typeof(string)))
271  {
272  return converter.ConvertFromInvariantString(attValue);
273  }
274  throw new ArgumentException(System.SR.GetString("Unable_to_convert_type_from_string", type.ToString()), "type");
275  }
276  default:
277  return null;
278  }
279  }
280 
281  private object SerializePropertyValue()
282  {
283  if (_Value == null)
284  {
285  return null;
286  }
288  {
289  return ConvertObjectToString(_Value, Property.PropertyType, Property.SerializeAs, Property.ThrowOnErrorSerializing);
290  }
291  MemoryStream memoryStream = new MemoryStream();
292  try
293  {
294  BinaryFormatter binaryFormatter = new BinaryFormatter();
295  binaryFormatter.Serialize(memoryStream, _Value);
296  return memoryStream.ToArray();
297  }
298  finally
299  {
300  memoryStream.Close();
301  }
302  }
303 
304  private static string ConvertObjectToString(object propValue, Type type, SettingsSerializeAs serializeAs, bool throwOnError)
305  {
306  if (serializeAs == SettingsSerializeAs.ProviderSpecific)
307  {
308  serializeAs = ((!(type == typeof(string)) && !type.IsPrimitive) ? SettingsSerializeAs.Xml : SettingsSerializeAs.String);
309  }
310  try
311  {
312  switch (serializeAs)
313  {
314  case SettingsSerializeAs.String:
315  {
316  TypeConverter converter = TypeDescriptor.GetConverter(type);
317  if (converter == null || !converter.CanConvertTo(typeof(string)) || !converter.CanConvertFrom(typeof(string)))
318  {
319  throw new ArgumentException(System.SR.GetString("Unable_to_convert_type_to_string", type.ToString()), "type");
320  }
321  return converter.ConvertToInvariantString(propValue);
322  }
323  case SettingsSerializeAs.Binary:
324  {
325  MemoryStream memoryStream = new MemoryStream();
326  try
327  {
328  BinaryFormatter binaryFormatter = new BinaryFormatter();
329  binaryFormatter.Serialize(memoryStream, propValue);
330  byte[] inArray = memoryStream.ToArray();
331  return Convert.ToBase64String(inArray);
332  }
333  finally
334  {
335  memoryStream.Close();
336  }
337  }
338  case SettingsSerializeAs.Xml:
339  {
340  XmlSerializer xmlSerializer = new XmlSerializer(type);
342  xmlSerializer.Serialize(stringWriter, propValue);
343  return stringWriter.ToString();
344  }
345  }
346  }
347  catch (Exception)
348  {
349  if (throwOnError)
350  {
351  throw;
352  }
353  }
354  return null;
355  }
356  }
357 }
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
bool IsPrimitive
Gets a value indicating whether the T:System.Type is one of the primitive types.
Definition: Type.cs:690
bool ThrowOnErrorSerializing
Gets or sets a value specifying whether an error will be thrown when the property is unsuccessfully s...
Describes a set of security permissions applied to code. This class cannot be inherited.
virtual object DefaultValue
Gets or sets the default value of the T:System.Configuration.SettingsProperty object.
Used internally as the class that represents metadata about an individual configuration property.
Implements a T:System.IO.TextWriter for writing information to a string. The information is stored in...
Definition: StringWriter.cs:13
static TypeConverter GetConverter(object component)
Returns a type converter for the type of the specified component.
void Serialize(Stream serializationStream, object graph)
Serializes the object, or graph of objects with the specified top (root), to the given stream.
BindingFlags
Specifies flags that control binding and the way in which the search for members and types is conduct...
Definition: BindingFlags.cs:10
Definition: __Canon.cs:3
Represents an instant in time, typically expressed as a date and time of day. To browse the ....
Definition: DateTime.cs:13
object Deserialize(Stream serializationStream)
Deserializes the specified stream into an object graph.
static AppDomain CurrentDomain
Gets the current application domain for the current T:System.Threading.Thread.
Definition: AppDomain.cs:274
Represents an application domain, which is an isolated environment where applications execute....
Definition: AppDomain.cs:33
Serializes and deserializes an object, or an entire graph of connected objects, in binary format.
SettingsProperty Property
Gets the T:System.Configuration.SettingsProperty object.
bool UsingDefaultValue
Gets a Boolean value specifying whether the value of the T:System.Configuration.SettingsPropertyValue...
SettingsPropertyValue(SettingsProperty property)
Initializes a new instance of the T:System.Configuration.SettingsPropertyValue class,...
SecurityAction
Specifies the security actions that can be performed using declarative security.
virtual void Close()
Closes the current stream and releases any resources (such as sockets and file handles) associated wi...
Definition: Stream.cs:855
Creates a stream whose backing store is memory.To browse the .NET Framework source code for this type...
Definition: MemoryStream.cs:13
override string ToString()
Returns a string containing the characters written to the current StringWriter so far.
virtual byte [] ToArray()
Writes the stream contents to a byte array, regardless of the P:System.IO.MemoryStream....
A cast or conversion operation, such as (SampleType)obj in C::or CType(obj, SampleType) in Visual Bas...
Serializes and deserializes objects into and from XML documents. The T:System.Xml....
Definition: SR.cs:7
Provides information about the characteristics for a component, such as its attributes,...
bool Deserialized
Gets or sets whether the value of a T:System.Configuration.SettingsProperty object has been deseriali...
Contains the value of a settings property that can be loaded and stored by an instance of T:System....
bool IsValueType
Gets a value indicating whether the T:System.Type is a value type.
Definition: Type.cs:445
object SerializedValue
Gets or sets the serialized value of the T:System.Configuration.SettingsProperty object.
virtual Type PropertyType
Gets or sets the type for the T:System.Configuration.SettingsProperty.
bool IsDirty
Gets or sets whether the value of a T:System.Configuration.SettingsProperty object has changed.
object Deserialize(Stream stream)
Deserializes the XML document contained by the specified T:System.IO.Stream.
virtual SettingsSerializeAs SerializeAs
Gets or sets a T:System.Configuration.SettingsSerializeAs object for the T:System....
string Name
Gets the name of the property from the associated T:System.Configuration.SettingsProperty object.
object GetData(string name)
Gets the value stored in the current application domain for the specified name.
Definition: AppDomain.cs:2542
void Serialize(TextWriter textWriter, object o)
Serializes the specified T:System.Object and writes the XML document to a file using the specified T:...
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
SecurityPermissionFlag
Specifies access flags for the security permission object.
object PropertyValue
Gets or sets the value of the T:System.Configuration.SettingsProperty object.
SettingsSerializeAs
Determines the serialization scheme used to store application settings.
Implements a T:System.IO.TextReader that reads from a string.
Definition: StringReader.cs:10
Provides a unified way of converting types of values to other types, as well as for accessing standar...
virtual bool IsAssignableFrom(Type c)
Determines whether an instance of a specified type can be assigned to an instance of the current type...
Definition: Type.cs:2707
virtual string Name
Gets or sets the name of the T:System.Configuration.SettingsProperty.