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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
//------------------------------------------------------------------------------
// <copyright file="SettingsPropertyValue.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Configuration {
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.Serialization;
using System.Configuration.Provider;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Security.Permissions;
using System.Reflection;
using System.Runtime.Versioning;
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
public class SettingsPropertyValue
{
public string Name { get { return _Property.Name; } }
public bool IsDirty { get { return _IsDirty; } set { _IsDirty = value; }}
public SettingsProperty Property { get { return _Property; } }
public bool UsingDefaultValue { get { return _UsingDefaultValue; } }
public SettingsPropertyValue(SettingsProperty property)
{
_Property = property;
}
public object PropertyValue
{
get
{
if (!_Deserialized)
{
_Value = Deserialize();
_Deserialized = true;
}
if (_Value != null && !Property.PropertyType.IsPrimitive && !(_Value is string) && !(_Value is DateTime))
{
_UsingDefaultValue = false;
_ChangedSinceLastSerialized = true;
_IsDirty = true;
}
return _Value;
}
set
{
_Value = value;
_IsDirty = true;
_ChangedSinceLastSerialized = true;
_Deserialized = true;
_UsingDefaultValue = false;
}
}
public object SerializedValue
{
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
get {
if (_ChangedSinceLastSerialized) {
_ChangedSinceLastSerialized = false;
_SerializedValue = SerializePropertyValue();
}
return _SerializedValue;
}
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
set {
_UsingDefaultValue = false;
_SerializedValue = value;
}
}
public bool Deserialized
{
get { return _Deserialized; }
set { _Deserialized = value; }
}
[ResourceExposure(ResourceScope.None)]
[ResourceConsumption(ResourceScope.AppDomain, ResourceScope.AppDomain)]
private bool IsHostedInAspnet() {
// See System.Web.Hosting.ApplicationManager::PopulateDomainBindings
return AppDomain.CurrentDomain.GetData(".appDomain") != null;
}
private object Deserialize()
{
object val = null;
//////////////////////////////////////////////
/// Step 1: Try creating from Serailized value
if (SerializedValue != null)
{
try {
if (SerializedValue is string) {
val = GetObjectFromString(Property.PropertyType, Property.SerializeAs, (string)SerializedValue);
} else {
MemoryStream ms = new System.IO.MemoryStream((byte[])SerializedValue);
try {
val = (new BinaryFormatter()).Deserialize(ms);
} finally {
ms.Close();
}
}
}
catch (Exception exception) {
try {
if (IsHostedInAspnet()) {
object[] args = new object[] { Property, this, exception};
const string webBaseEventTypeName = "System.Web.Management.WebBaseEvent, " + AssemblyRef.SystemWeb;
Type type = Type.GetType(webBaseEventTypeName, true);
type.InvokeMember("RaisePropertyDeserializationWebErrorEvent",
BindingFlags.NonPublic|BindingFlags.Static|BindingFlags.InvokeMethod,
null, null, args, CultureInfo.InvariantCulture);
}
}
catch {
}
}
if (val != null && !Property.PropertyType.IsAssignableFrom(val.GetType())) // is it the correct type
val = null;
}
//////////////////////////////////////////////
/// Step 2: Try creating from default value
if (val == null)
{
_UsingDefaultValue = true;
if (Property.DefaultValue == null || Property.DefaultValue.ToString() == "[null]") {
if (Property.PropertyType.IsValueType)
return SecurityUtils.SecureCreateInstance(Property.PropertyType);
else
return null;
}
if (!(Property.DefaultValue is string)) {
val = Property.DefaultValue;
} else {
try {
val = GetObjectFromString(Property.PropertyType, Property.SerializeAs, (string)Property.DefaultValue);
} catch(Exception e) {
throw new ArgumentException(SR.GetString(SR.Could_not_create_from_default_value, Property.Name, e.Message));
}
}
if (val != null && !Property.PropertyType.IsAssignableFrom(val.GetType())) // is it the correct type
throw new ArgumentException(SR.GetString(SR.Could_not_create_from_default_value_2, Property.Name));
}
//////////////////////////////////////////////
/// Step 3: Create a new one by calling the parameterless constructor
if (val == null)
{
if (Property.PropertyType == typeof(string)) {
val = "";
} else {
try {
val = SecurityUtils.SecureCreateInstance(Property.PropertyType);
} catch {}
}
}
return val;
}
private static object GetObjectFromString(Type type, SettingsSerializeAs serializeAs, string attValue)
{
// Deal with string types
if (type == typeof(string) && (attValue == null || attValue.Length < 1 || serializeAs == SettingsSerializeAs.String))
return attValue;
// Return null if there is nothing to convert
if (attValue == null || attValue.Length < 1)
return null;
// Convert based on the serialized type
switch (serializeAs)
{
case SettingsSerializeAs.Binary:
byte[] buf = Convert.FromBase64String(attValue);
MemoryStream ms = null;
try {
ms = new System.IO.MemoryStream(buf);
return (new BinaryFormatter()).Deserialize(ms);
} finally {
if (ms != null)
ms.Close();
}
case SettingsSerializeAs.Xml:
StringReader sr = new StringReader(attValue);
XmlSerializer xs = new XmlSerializer(type);
return xs.Deserialize(sr);
case SettingsSerializeAs.String:
TypeConverter converter = TypeDescriptor.GetConverter(type);
if (converter != null && converter.CanConvertTo(typeof(String)) && converter.CanConvertFrom(typeof(String)))
return converter.ConvertFromInvariantString(attValue);
throw new ArgumentException(SR.GetString(SR.Unable_to_convert_type_from_string, type.ToString()), "type");
default:
return null;
}
}
private object SerializePropertyValue()
{
if (_Value == null)
return null;
if (Property.SerializeAs != SettingsSerializeAs.Binary)
return ConvertObjectToString(_Value, Property.PropertyType, Property.SerializeAs, Property.ThrowOnErrorSerializing);
MemoryStream ms = new System.IO.MemoryStream();
try {
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, _Value);
return ms.ToArray();
} finally {
ms.Close();
}
}
private static string ConvertObjectToString(object propValue, Type type, SettingsSerializeAs serializeAs, bool throwOnError)
{
if (serializeAs == SettingsSerializeAs.ProviderSpecific) {
if (type == typeof(string) || type.IsPrimitive)
serializeAs = SettingsSerializeAs.String;
else
serializeAs = SettingsSerializeAs.Xml;
}
try {
switch (serializeAs) {
case SettingsSerializeAs.String:
TypeConverter converter = TypeDescriptor.GetConverter(type);
if (converter != null && converter.CanConvertTo(typeof(String)) && converter.CanConvertFrom(typeof(String)))
return converter.ConvertToInvariantString(propValue);
throw new ArgumentException(SR.GetString(SR.Unable_to_convert_type_to_string, type.ToString()), "type");
case SettingsSerializeAs.Binary :
MemoryStream ms = new System.IO.MemoryStream();
try {
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, propValue);
byte[] buffer = ms.ToArray();
return Convert.ToBase64String(buffer);
} finally {
ms.Close();
}
case SettingsSerializeAs.Xml :
XmlSerializer xs = new XmlSerializer(type);
StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
xs.Serialize(sw, propValue);
return sw.ToString();
}
} catch (Exception) {
if (throwOnError)
throw;
}
return null;
}
private object _Value = null;
private object _SerializedValue = null;
private bool _Deserialized = false;
private bool _IsDirty = false;
private SettingsProperty _Property = null;
private bool _ChangedSinceLastSerialized = false;
private bool _UsingDefaultValue = true;
}
}
|