public Object GetProperty( String propertyName, SettingsSerializeAs serializeAs) { bool lazyLoad = false; return GetProperty(propertyName, serializeAs, lazyLoad); }
public SettingsProperty( string name, Type propertyType, SettingsProvider provider, bool isReadOnly, object defaultValue, SettingsSerializeAs serializeAs, SettingsAttributeDictionary attributes, bool throwOnErrorDeserializing, bool throwOnErrorSerializing) { Name = name; PropertyType = propertyType; Provider = provider; IsReadOnly = isReadOnly; DefaultValue = defaultValue; #pragma warning disable CS0618 // Type or member is obsolete if (serializeAs == SettingsSerializeAs.Binary) #pragma warning restore CS0618 // Type or member is obsolete { if (EnableUnsafeBinaryFormatterInPropertyValueSerialization) { SerializeAs = serializeAs; } else { throw new NotSupportedException(Obsoletions.BinaryFormatterMessage); } } Attributes = attributes; ThrowOnErrorDeserializing = throwOnErrorDeserializing; ThrowOnErrorSerializing = throwOnErrorSerializing; }
private static object Deserialize(SettingsPropertyValue p, string s) { if (p == null) { throw new ArgumentNullException("p"); } Type type = p.Property.PropertyType; SettingsSerializeAs serializeAs = p.Property.SerializeAs; object ret = null; if (type == typeof(string) && (string.IsNullOrEmpty(s) || (serializeAs == SettingsSerializeAs.String))) { return(s); } else if (serializeAs == SettingsSerializeAs.String) { TypeConverter converter = TypeDescriptor.GetConverter(type); if (converter == null || !converter.CanConvertTo(typeof(string)) || !converter.CanConvertFrom(typeof(string))) { throw new ArgumentException("Cannot convert type!"); } ret = converter.ConvertFromInvariantString(s); } else if (serializeAs == SettingsSerializeAs.Xml) { StringReader reader = new StringReader(s); XmlSerializer serializer = new XmlSerializer(type); ret = serializer.Deserialize(reader); } else { throw new ProviderException("The provider does not support binary serialization because of security constraints!"); } return(ret); }
} // LoadValue // ---------------------------------------------------------------------- protected object LoadValue(string name, Type type, SettingsSerializeAs serializeAs, object defaultValue) { CreateSettingProperty(name, type, serializeAs, defaultValue); object value = ApplicationSettings[name]; return(OnValueLoading(name, value)); } // LoadValue
public SettingElement(string name, SettingsSerializeAs serializeAs) { #if CONFIGURATION_DEP Name = name; SerializeAs = serializeAs; #endif }
public SettingElement (string name, SettingsSerializeAs serializeAs) { #if CONFIGURATION_DEP Name = name; SerializeAs = serializeAs; #endif }
private object GetPropertyRecursively(DirectoryEntry userEntry, string propertyToGet, Type destinationType, SettingsSerializeAs serializeAs, AttributeMappingList attributeListForContext) { //Get the Active Directory User attribute mapping AttributeMapping attrMap = attributeListForContext[propertyToGet]; //if no such key exists i.e. attrMap is null, simply return null; //Primitives, string, simple collections and serialized complex types if (destinationType.IsPrimitive || destinationType.Equals(typeof(string)) || typeof(ICollection).IsAssignableFrom(destinationType) || destinationType.IsArray || destinationType.IsValueType) { //Get value from Active Directory and convert it into correct type object convertedObject = GetConvertedAttributeValueForUser(userEntry, attrMap.ActiveDirectoryUserAttribute, destinationType); return(convertedObject); } //Complex types else { if (serializeAs != SettingsSerializeAs.ProviderSpecific) { if (serializeAs == SettingsSerializeAs.String || serializeAs == SettingsSerializeAs.Xml) { return(GetConvertedAttributeValueForUser(userEntry, attrMap.ActiveDirectoryUserAttribute, typeof(string))); } else { return(GetConvertedAttributeValueForUser(userEntry, attrMap.ActiveDirectoryUserAttribute, typeof(byte[]))); } } else { //Get the mappings for the inner properties AttributeMappingList innerMappings = attributeListForContext[propertyToGet].InnerMappingList; //Create a new instance of the complex type using Activator.CreateInstance and default constructor. object complexObj = Activator.CreateInstance(destinationType); //Call AD to get value for each inner property foreach (AttributeMapping innerMapping in innerMappings) { //Get the current property PropertyInfo propInfo = destinationType.GetProperty(innerMapping.ProfileAttribute); //Recursively call BuildComplexObject object convertedObject = GetPropertyRecursively(userEntry, innerMapping.ProfileAttribute, propInfo.PropertyType, SettingsSerializeAs.ProviderSpecific, innerMappings); //Set the property of complex type propInfo.SetValue(complexObj, convertedObject, null); } return(complexObj); } } }
/// <summary> /// Converts the object to string. /// </summary> /// <param name="propValue">The prop value.</param> /// <param name="type">The type.</param> /// <param name="serializeAs">The serialize as.</param> /// <param name="throwOnError">if set to <c>true</c> [throw on error].</param> /// <returns></returns> public 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.ConvertToString(propValue)); } throw new ArgumentException("Unable to convert type " + type.ToString() + " to string", "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); }
public StateErrorFilter(Type item, SettingsSerializeAs map) { //Discarded unreachable code: IL_0002, IL_0006 //IL_0003: Incompatible stack heights: 0 vs 1 //IL_0007: Incompatible stack heights: 0 vs 1 SingletonReader.PushGlobal(); base._002Ector(); m_ContextComposer = item; _ParamComposer = map; }
} // SaveValue // ---------------------------------------------------------------------- protected void SaveValue(string name, Type type, SettingsSerializeAs serializeAs, object value, object defaultValue) { if (OnValueSaving(name, value) == null) { return; } CreateSettingPropertyValue(name, type, serializeAs, defaultValue); ApplicationSettings[name] = value; } // SaveValue
public SettingsProperty(string name, Type propertyType, SettingsProvider provider, bool isReadOnly, object defaultValue, SettingsSerializeAs serializeAs, SettingsAttributeDictionary attributes, bool throwOnErrorDeserializing, bool throwOnErrorSerializing) { this._Name = name; this._PropertyType = propertyType; this._Provider = provider; this._IsReadOnly = isReadOnly; this._DefaultValue = defaultValue; this._SerializeAs = serializeAs; this._Attributes = attributes; this._ThrowOnErrorDeserializing = throwOnErrorDeserializing; this._ThrowOnErrorSerializing = throwOnErrorSerializing; }
public SettingsProperty(SettingsProperty propertyToCopy) { this._Name = propertyToCopy.Name; this._IsReadOnly = propertyToCopy.IsReadOnly; this._DefaultValue = propertyToCopy.DefaultValue; this._SerializeAs = propertyToCopy.SerializeAs; this._Provider = propertyToCopy.Provider; this._PropertyType = propertyToCopy.PropertyType; this._ThrowOnErrorDeserializing = propertyToCopy.ThrowOnErrorDeserializing; this._ThrowOnErrorSerializing = propertyToCopy.ThrowOnErrorSerializing; this._Attributes = new SettingsAttributeDictionary(propertyToCopy.Attributes); }
private static object GetObjectFromString(Type type, SettingsSerializeAs serializeAs, string attValue) { StringReader reader; if ((type == typeof(string)) && (((attValue == null) || (attValue.Length < 1)) || (serializeAs == SettingsSerializeAs.String))) { return(attValue); } if ((attValue != null) && (attValue.Length >= 1)) { switch (serializeAs) { case SettingsSerializeAs.String: { TypeConverter converter = TypeDescriptor.GetConverter(type); if (((converter == null) || !converter.CanConvertTo(typeof(string))) || !converter.CanConvertFrom(typeof(string))) { throw new ArgumentException(System.SR.GetString("Unable_to_convert_type_from_string", new object[] { type.ToString() }), "type"); } return(converter.ConvertFromInvariantString(attValue)); } case SettingsSerializeAs.Xml: goto Label_0078; case SettingsSerializeAs.Binary: { byte[] buffer = Convert.FromBase64String(attValue); MemoryStream serializationStream = null; try { serializationStream = new MemoryStream(buffer); return(new BinaryFormatter().Deserialize(serializationStream)); } finally { if (serializationStream != null) { serializationStream.Close(); } } goto Label_0078; } } } return(null); Label_0078: reader = new StringReader(attValue); XmlSerializer serializer = new XmlSerializer(type); return(serializer.Deserialize(reader)); }
private static object GetObjectFromString(Type type, SettingsSerializeAs serializeAs, string serializedValue) { // Deal with string types if (type == typeof(string) && (serializedValue == null || serializedValue.Length < 1 || serializeAs == SettingsSerializeAs.String)) { return(serializedValue); } // Return null if there is nothing to convert if (serializedValue == null || serializedValue.Length < 1) { return(null); } // Convert based on the serialized type switch (serializeAs) { #pragma warning disable CS0618 // Type or member is obsolete case SettingsSerializeAs.Binary: #pragma warning restore CS0618 // Type or member is obsolete if (SettingsProperty.EnableUnsafeBinaryFormatterInPropertyValueSerialization) { byte[] buffer = Convert.FromBase64String(serializedValue); using (MemoryStream ms = new MemoryStream(buffer)) { #pragma warning disable SYSLIB0011 // BinaryFormatter serialization is obsolete and should not be used. return((new BinaryFormatter()).Deserialize(ms)); #pragma warning restore SYSLIB0011 } } else { throw new NotSupportedException(Obsoletions.BinaryFormatterMessage); } case SettingsSerializeAs.Xml: StringReader sr = new StringReader(serializedValue); 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(serializedValue)); } throw new ArgumentException(SR.Format(SR.Unable_to_convert_type_from_string, type), nameof(type)); default: return(null); } }
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 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(string.Format(SR.Unable_to_convert_type_from_string, type.ToString()), "type"); default: return(null); } }
/// <summary> /// Gets the object from string. /// </summary> /// <param name="type">The type.</param> /// <param name="serializeAs">The serialize as.</param> /// <param name="attValue">The att value.</param> /// <returns></returns> public static object GetObjectFromString(Type type, SettingsSerializeAs serializeAs, string attValue) { // Deal with string types if (type == typeof(string) && (string.IsNullOrEmpty(attValue) == true || attValue.Length < 1 || serializeAs == SettingsSerializeAs.String)) { return(attValue); } // Return null if there is nothing to convert if (string.IsNullOrEmpty(attValue) == true) { 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.ConvertFromString(attValue)); } throw new ArgumentException("Unable to convert type: " + type.ToString() + " from string", "type"); default: return(null); } }
private static string ConvertObjectToString(object propertyValue, 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(propertyValue)); } throw new ArgumentException(SR.Format(SR.Unable_to_convert_type_to_string, type), nameof(type)); case SettingsSerializeAs.Xml: XmlSerializer xs = new XmlSerializer(type); StringWriter sw = new StringWriter(CultureInfo.InvariantCulture); xs.Serialize(sw, propertyValue); return(sw.ToString()); #pragma warning disable CS0618 // Type or member is obsolete case SettingsSerializeAs.Binary: #pragma warning restore CS0618 // Type or member is obsolete Debug.Fail("Should not have gotten here with Binary formatting"); break; } } catch (Exception) { if (throwOnError) { throw; } } return(null); }
public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection) { SettingsPropertyValueCollection ret = new SettingsPropertyValueCollection(); //Get the user name string userName = context["UserName"] as string; //Get the DirectoryEntry for the user name using (DirectoryEntry userEntry = GetUserEntryByUserName(userName)) { //Get each property IEnumerator en = collection.GetEnumerator(); while (en.MoveNext()) { //Property to get string propertyToGet = (en.Current as SettingsProperty).Name; //Get the .NET type for the property, change attrValue to the correct type Type detinationType = (en.Current as SettingsProperty).PropertyType; //Get the SerializeAs of the property SettingsSerializeAs serializeAs = (en.Current as SettingsProperty).SerializeAs; //Create a SettingsPropertyValue and for setting its value SettingsPropertyValue spv = new SettingsPropertyValue((en.Current as SettingsProperty)); //Build the current property recursively because it might be a complex object. if (serializeAs == SettingsSerializeAs.ProviderSpecific) { spv.PropertyValue = GetPropertyRecursively(userEntry, propertyToGet, detinationType, serializeAs, AttributeList); spv.Deserialized = true; } else { spv.SerializedValue = GetPropertyRecursively(userEntry, propertyToGet, detinationType, serializeAs, AttributeList); spv.Deserialized = false; } //Add the settingsPropertyValue to return collection ret.Add(spv); } //Set last actvity date if (LastActivityDateAttribute != null) { userEntry.InvokeSet(LastActivityDateAttribute, DateTime.Now); } } return(ret); }
public void CreateSettingPropertyValue(string name, Type type, SettingsSerializeAs serializeAs, object defaultValue) { CreateSettingProperty(name, type, serializeAs, defaultValue); var settingsProperty = PropertyValues[name]; if (settingsProperty != null) { return; // already present } var settingsPropertyValue = new SettingsPropertyValue(Properties[name]); PropertyValues.Add(settingsPropertyValue); }
protected void CreateSettingProperty(string name, Type type, SettingsSerializeAs serializeAs, object defaultValue) { ApplicationSettings _applicationSettings = ApplicationSettings; if (_applicationSettings == null || _applicationSettings.DefaultProvider == null) { return; } SettingsProperty _settingsProperty = _applicationSettings.Properties[name]; if (_settingsProperty != null) { return; // already present } SettingsAttributeDictionary _attributes = new SettingsAttributeDictionary(); SettingAttribute _attribute; switch (Scope) { case SettingScope.Application: // attribute = new ApplicationScopedSettingAttribute(); throw new NotImplementedException(); // currently not supported case SettingScope.User: _attribute = new UserScopedSettingAttribute(); break; default: return; } _attributes.Add(_attribute.TypeId, _attribute); _settingsProperty = new SettingsProperty( name, // name type, // type _applicationSettings.DefaultProvider, // settings provider false, // is readonly defaultValue, // default serializeAs, // serialize as _attributes, // attribute ThrowOnErrorDeserializing, // throw on deserialization ThrowOnErrorSerializing); // throw on serialization _applicationSettings.Properties.Add(_settingsProperty); }
protected void CreateSettingPropertyValue(string name, Type type, SettingsSerializeAs serializeAs, object defaultValue) { CreateSettingProperty(name, type, serializeAs, defaultValue); ApplicationSettings _applicationSettings = ApplicationSettings; SettingsProperty _settingsProperty = _applicationSettings.Properties[name]; if (_settingsProperty != null) { return; // already present } SettingsPropertyValue settingsPropertyValue = new SettingsPropertyValue(_settingsProperty); _applicationSettings.PropertyValues.Add(settingsPropertyValue); }
private static void AddToColl(ProfilePropertyMetadata p, SettingsPropertyCollection retColl, bool isAuthenticated) { string propName = p.PropertyName; Type propType = Type.GetType(p.TypeName, false, true); bool allowAnon = p.AllowAnonymousAccess; bool readOnly = p.IsReadOnly; if (!allowAnon && !isAuthenticated) { return; } SettingsSerializeAs serializeAs = (SettingsSerializeAs)p.SerializeAs; SettingsAttributeDictionary dict = new SettingsAttributeDictionary(); dict.Add("AllowAnonymous", allowAnon); retColl.Add(new SettingsProperty(propName, propType, null, readOnly, p.DefaultValue, serializeAs, dict, true, true)); }
private static object GetObjectFromString(Type type, SettingsSerializeAs serializeAs, string serializedValue) { // Deal with string types if (type == typeof(string) && (serializedValue == null || serializedValue.Length < 1 || serializeAs == SettingsSerializeAs.String)) { return(serializedValue); } // Return null if there is nothing to convert if (serializedValue == null || serializedValue.Length < 1) { return(null); } // Convert based on the serialized type switch (serializeAs) { case SettingsSerializeAs.Binary: byte[] buffer = Convert.FromBase64String(serializedValue); using (MemoryStream ms = new MemoryStream(buffer)) { // Issue https://github.com/dotnet/runtime/issues/39295 tracks finding an alternative to BinaryFormatter return((new BinaryFormatter()).Deserialize(ms)); } case SettingsSerializeAs.Xml: StringReader sr = new StringReader(serializedValue); 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(serializedValue)); } throw new ArgumentException(SR.Format(SR.Unable_to_convert_type_from_string, type), nameof(type)); default: return(null); } }
} // OnValueLoading // ---------------------------------------------------------------------- protected void CreateSettingProperty(string name, Type type, SettingsSerializeAs serializeAs, object defaultValue) { ApplicationSettings applicationSettings = ApplicationSettings; if (applicationSettings == null || applicationSettings.DefaultProvider == null) { return; } SettingsProperty settingsProperty = applicationSettings.Properties[name]; if (settingsProperty != null) { return; // already present } SettingsAttributeDictionary attributes = new SettingsAttributeDictionary(); SettingAttribute attribute; switch (Scope) { case SettingScope.Application: // attribute = new ApplicationScopedSettingAttribute(); throw new NotImplementedException(); // currently not supported case SettingScope.User: attribute = new UserScopedSettingAttribute(); break; default: return; } attributes.Add(attribute.TypeId, attribute); settingsProperty = new SettingsProperty( name, // name type, // type applicationSettings.DefaultProvider, // settings provider false, // is readonly defaultValue, // default serializeAs, // serialize as attributes, // attribute ThrowOnErrorDeserializing, // throw on deserialization ThrowOnErrorSerializing); // throw on serialization applicationSettings.Properties.Add(settingsProperty); } // CreateSettingProperty
public void CreateSettingProperty(string name, Type type, SettingsSerializeAs serializeAs, object defaultValue) { var settingsProperty = Properties[name]; if (settingsProperty != null) { return; } var attributes = new SettingsAttributeDictionary(); var attribute = new UserScopedSettingAttribute(); attributes.Add(attribute.TypeId, attribute); settingsProperty = new SettingsProperty( name, type, DefaultProvider, false, defaultValue, serializeAs, attributes, false, false); Properties.Add(settingsProperty); }
} // public SortableSettingsProperty (2 of 3) /// <summary> /// Construct an instance in which every property is fully specified. /// </summary> /// <param name="name"> /// The name of the SettingsProperty object /// </param> /// <param name="propertyType"> /// the type of SettingsProperty object /// </param> /// <param name="provider"> /// A SettingsProvider object to use for persistence /// </param> /// <param name="isReadOnly"> /// A Boolean value specifying whether the SettingsProperty object is /// read-only /// </param> /// <param name="defaultValue"> /// The default value of the SettingsProperty object /// </param> /// <param name="serializeAs"> /// A SettingsSerializeAs object /// /// This object is an enumeration used to set the serialization scheme /// for storing application settings. /// </param> /// <param name="attributes"> /// A SettingsAttributeDictionary object /// </param> /// <param name="throwOnErrorDeserializing"> /// A Boolean value specifying whether an error will be thrown when the /// property is unsuccessfully deserialized /// </param> /// <param name="throwOnErrorSerializing"> /// A Boolean value specifying whether an error will be thrown when the /// property is unsuccessfully serialized /// </param> public SortableSettingsProperty ( string name , Type propertyType , SettingsProvider provider , bool isReadOnly , object defaultValue , SettingsSerializeAs serializeAs , SettingsAttributeDictionary attributes , bool throwOnErrorDeserializing , bool throwOnErrorSerializing ) : base ( name , // string name propertyType , // Type propertType, provider , // SettingsProvider provider isReadOnly , // bool isReadOnly defaultValue , // object defaultValue serializeAs , // SettingsSerializeAs serializeAs attributes , // SettingsAttributeDictionary attributes throwOnErrorDeserializing , // bool throwOnErrorDeserializing throwOnErrorSerializing ) // bool throwOnErrorSerializing { } // public SortableSettingsProperty (3 of 3)
} // LoadValue // ---------------------------------------------------------------------- protected void SaveValue(string name, Type type, SettingsSerializeAs serializeAs, object value) { SaveValue(name, type, serializeAs, value, null); } // SaveValue
public SettingsProperty(string name, Type propertyType, SettingsProvider provider, bool isReadOnly, object defaultValue, SettingsSerializeAs serializeAs, SettingsAttributeDictionary attributes, bool throwOnErrorDeserializing, bool throwOnErrorSerializing) { }
internal StoredSetting(SettingsSerializeAs serializeAs, XmlNode value) { SerializeAs = serializeAs; Value = value; }
public SettingElement(string name, SettingsSerializeAs serializeAs) : this() { this.Name = name; this.SerializeAs = serializeAs; }
private Object GetLazyLoadedProperty( String propertyName, SettingsSerializeAs serializeAs) { object result = null; using (IDataReader reader = DBSiteUser.GetLazyLoadedProperty(this.userGuid, propertyName)) { if (reader.Read()) { switch (serializeAs) { case SettingsSerializeAs.String: if (reader["PropertyName"].ToString() == propertyName) { result = reader["PropertyValueString"].ToString(); } break; default: if (reader["PropertyName"].ToString() == propertyName) { result = reader["PropertyValueBinary"]; } break; } } } return result; }
private void UpdateProperty( String propertyName, Object propertyValue, SettingsSerializeAs serializeAs, bool lazyLoad) { switch (serializeAs) { case SettingsSerializeAs.String: default: // currently only serializing to string DBSiteUser.UpdateProperty( this.userGuid, propertyName, propertyValue.ToString(), null, DateTime.UtcNow, lazyLoad); break; } }
} // CreateSettingProperty // ---------------------------------------------------------------------- protected void CreateSettingPropertyValue(string name, Type type, SettingsSerializeAs serializeAs, object defaultValue) { CreateSettingProperty(name, type, serializeAs, defaultValue); ApplicationSettings applicationSettings = ApplicationSettings; SettingsProperty settingsProperty = applicationSettings.Properties[name]; if (settingsProperty != null) { return; // already present } SettingsPropertyValue settingsPropertyValue = new SettingsPropertyValue(settingsProperty); applicationSettings.PropertyValues.Add(settingsPropertyValue); } // CreateSettingPropertyValue
public void SetProperty( String propertyName, Object propertyValue, SettingsSerializeAs serializeAs, bool lazyLoad) { switch (propertyName) { //the properties identified in the case statements //were already implemented on SiteUser //before the exstensible profile was implemented case "UserID": //no change allowed through profile break; case "SiteID": //no change allowed through profile break; case "IsDeleted": //no change allowed through profile break; case "UserGuid": //no change allowed through profile break; case "RegisterConfirmGuid": //no change allowed through profile break; case "TimeZoneId": if (propertyValue is string) { this.TimeZoneId = propertyValue.ToString(); this.Save(); } break; case "FirstName": if (propertyValue is String) { this.FirstName = propertyValue.ToString(); this.Save(); } break; case "LastName": if (propertyValue is String) { this.LastName = propertyValue.ToString(); this.Save(); } break; case "PhoneNumber": if (propertyValue is String) this.PhoneNumber = propertyValue.ToString(); this.Save(); break; case "AuthorBio": if (propertyValue is String) { this.AuthorBio = propertyValue.ToString(); this.Save(); } break; case "Name": if (propertyValue is String) { this.Name = propertyValue.ToString(); this.Save(); } break; case "LoginName": //no change allowed through profile break; case "Email": //no change allowed through profile break; case "Password": //no change allowed through profile break; case "Gender": if (propertyValue is String) { this.Gender = propertyValue.ToString(); this.Save(); } break; case "ProfileApproved": //no change allowed through profile break; case "ApprovedForForums": //no change allowed through profile break; case "Trusted": //no change allowed through profile break; case "DisplayInMemberList": //no change allowed through profile break; case "WebSiteUrl": if (propertyValue is String) { this.WebSiteUrl = propertyValue.ToString(); if (this.webSiteUrl.Length > 100) { this.webSiteUrl = this.webSiteUrl.Substring(0, 100); } this.Save(); } break; case "Country": if (propertyValue is String) { this.Country = propertyValue.ToString(); if (this.country.Length > 100) { this.country = this.country.Substring(0, 100); } this.Save(); } break; case "State": if (propertyValue is String) { this.State = propertyValue.ToString(); if (this.state.Length > 100) { this.state = this.state.Substring(0, 100); } this.Save(); } break; case "Occupation": if (propertyValue is String) { this.Occupation = propertyValue.ToString(); if (this.occupation.Length > 100) { this.occupation = this.occupation.Substring(0, 100); } this.Save(); } break; case "Interests": if (propertyValue is String) { this.Interests = propertyValue.ToString(); if (this.interests.Length > 100) { this.interests = this.interests.Substring(0, 100); } this.Save(); } break; case "MSN": if (propertyValue is String) { this.MSN = propertyValue.ToString(); if (this.msn.Length > 50) { this.msn = this.msn.Substring(0, 50); } this.Save(); } break; case "Yahoo": if (propertyValue is String) { this.Yahoo = propertyValue.ToString(); if (this.yahoo.Length > 50) { this.yahoo = this.yahoo.Substring(0, 50); } this.Save(); } break; case "AIM": if (propertyValue is String) { this.AIM = propertyValue.ToString(); if (this.aim.Length > 50) { this.aim = this.aim.Substring(0, 50); } this.Save(); } break; case "ICQ": if (propertyValue is String) { this.ICQ = propertyValue.ToString(); if (this.icq.Length > 50) { this.icq = this.icq.Substring(0, 50); } this.Save(); } break; case "TotalPosts": //no change allowed through profile break; case "AvatarUrl": //no change allowed through profile break; //case "TimeOffsetHours": // //no change allowed through profile // break; case "Signature": if (propertyValue is String) { this.Signature = propertyValue.ToString(); this.Save(); } break; case "DateCreated": //no change allowed through profile break; case "Skin": //no change allowed through profile break; case "LoweredEmail": //no change allowed through profile break; case "PasswordQuestion": //no change allowed through profile break; case "PasswordAnswer": //no change allowed through profile break; case "LastActivityDate": //no change allowed through profile break; default: // this is for properties added to config //that were not previously implemented on SiteUser bool propertyExists = DBSiteUser.PropertyExists(this.userGuid, propertyName); if (!propertyExists) { CreateProperty(propertyName, propertyValue, serializeAs, lazyLoad); if (!lazyLoad) { CreatePropertyLocalInstance(propertyName, propertyValue, serializeAs); } } else { UpdateProperty(propertyName, propertyValue, serializeAs, lazyLoad); if (!lazyLoad) { UpdatePropertyLocalInstance(propertyName, propertyValue, serializeAs); } } break; } }
} // CreateSettingPropertyValue // ---------------------------------------------------------------------- protected object LoadValue(string name, Type type, SettingsSerializeAs serializeAs) { return LoadValue(name, type, serializeAs, null); } // LoadValue
} // CreateSettingPropertyValue // ---------------------------------------------------------------------- protected object LoadValue(string name, Type type, SettingsSerializeAs serializeAs) { return(LoadValue(name, type, serializeAs, null)); } // LoadValue
public SettingElement(string name, SettingsSerializeAs serializeAs) : this() { Name = name; SerializeAs = serializeAs; }
} // LoadValue // ---------------------------------------------------------------------- protected object LoadValue(string name, Type type, SettingsSerializeAs serializeAs, object defaultValue) { CreateSettingProperty(name, type, serializeAs, defaultValue); object value = ApplicationSettings[name]; return OnValueLoading(name, value); } // LoadValue
internal static string GetElementValue(XmlNode xmlNode, SettingsSerializeAs serializeAs) { return(serializeAs == SettingsSerializeAs.Xml ? xmlNode.InnerXml : xmlNode.InnerText); }
public Object GetProperty( String propertyName, SettingsSerializeAs serializeAs, bool lazyLoad) { switch (propertyName) { //the properties identified in the case statements //were already implemented on SiteUser //before the exstensible profile was implemented //so we just return the existing property for those //other properties are handled by the bottom default case //so arbitrary properties can be configured case "UserID": return this.UserId; case "SiteID": return this.SiteId; case "IsDeleted": return this.IsDeleted; case "UserGuid": return this.UserGuid; case "RegisterConfirmGuid": return this.RegisterConfirmGuid; case "FirstName": return this.FirstName; case "LastName": return this.LastName; case "PhoneNumber": return this.PhoneNumber; case "AuthorBio": return this.AuthorBio; case "DateOfBirth": if (this.DateOfBirth == DateTime.MinValue) { return string.Empty; } return this.DateOfBirth; case "Name": return this.Name; case "LoginName": return this.LoginName; case "Email": return this.Email; case "Password": // don't return the password from the profile object return String.Empty; case "Gender": return this.Gender; case "ProfileApproved": return this.ProfileApproved; case "ApprovedForLogin": return this.ApprovedForLogin; case "Trusted": return this.Trusted; case "DisplayInMemberList": return this.DisplayInMemberList; case "WebSiteUrl": return this.WebSiteUrl; case "Country": return this.Country; case "State": return this.State; case "Occupation": return this.Occupation; case "Interests": return this.Interests; case "MSN": return this.MSN; case "Yahoo": return this.Yahoo; case "AIM": return this.AIM; case "ICQ": return this.ICQ; case "TotalPosts": return this.TotalPosts; case "AvatarUrl": return this.AvatarUrl; case "TimeOffsetHours": EnsureNonLazyLoadedProperties(); object objTimeOffset = GetNonLazyLoadedProperty(propertyName, serializeAs); if ((objTimeOffset != null)&&(objTimeOffset.ToString().Length > 0)) { return objTimeOffset; } else { string timeOffset = "-5.00"; if (ConfigurationManager.AppSettings["PreferredGreenwichMeantimeOffset"] != null) { timeOffset = ConfigurationManager.AppSettings["PreferredGreenwichMeantimeOffset"]; } return timeOffset; } //return this.TimeOffsetHours; case "Signature": return this.Signature; case "DateCreated": return this.DateCreated; case "Skin": return this.Skin; case "LoweredEmail": return this.LoweredEmail; case "PasswordQuestion": return this.PasswordQuestion; case "PasswordAnswer": return this.PasswordAnswer; case "LastActivityDate": return this.LastActivityDate; default: // this is for properties added to config //that were not previously implemented on SiteUser if (!lazyLoad) { EnsureNonLazyLoadedProperties(); return GetNonLazyLoadedProperty(propertyName, serializeAs); } else { // lazyLoaded Properties are either seldom used or expensive // and therefore only loaded from the db as needed return GetLazyLoadedProperty(propertyName, serializeAs); } } }
public void SetProperty( String propertyName, Object propertyValue, SettingsSerializeAs serializeAs) { bool lazyLoad = false; SetProperty(propertyName, propertyValue, serializeAs, lazyLoad); }
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 { XmlSerializer serializer; switch (serializeAs) { case SettingsSerializeAs.String: { TypeConverter converter = TypeDescriptor.GetConverter(type); if (((converter == null) || !converter.CanConvertTo(typeof(string))) || !converter.CanConvertFrom(typeof(string))) { break; } return converter.ConvertToInvariantString(propValue); } case SettingsSerializeAs.Xml: goto Label_00D2; case SettingsSerializeAs.Binary: { MemoryStream serializationStream = new MemoryStream(); try { new BinaryFormatter().Serialize(serializationStream, propValue); return Convert.ToBase64String(serializationStream.ToArray()); } finally { serializationStream.Close(); } goto Label_00D2; } default: goto Label_0105; } throw new ArgumentException(System.SR.GetString("Unable_to_convert_type_to_string", new object[] { type.ToString() }), "type"); Label_00D2: serializer = new XmlSerializer(type); StringWriter writer = new StringWriter(CultureInfo.InvariantCulture); serializer.Serialize((TextWriter) writer, propValue); return writer.ToString(); } catch (Exception) { if (throwOnError) { throw; } } Label_0105: return null; }
private void CreatePropertyLocalInstance( String propertyName, Object propertyValue, SettingsSerializeAs serializeAs) { if ( (this.profileProperties != null) &&(nonLazyLoadedProfilePropertiesLoaded) ) { DataRow row = profileProperties.NewRow(); row["UserGuid"] = this.userGuid.ToString(); row["PropertyName"] = propertyName; switch (serializeAs) { case SettingsSerializeAs.String: row["PropertyValueString"] = propertyValue.ToString(); break; default: row["PropertyValueBinary"] = propertyValue; break; } profileProperties.Rows.Add(row); } }
private static object GetObjectFromString(Type type, SettingsSerializeAs serializeAs, string attValue) { StringReader reader; if ((type == typeof(string)) && (((attValue == null) || (attValue.Length < 1)) || (serializeAs == SettingsSerializeAs.String))) { return attValue; } if ((attValue != null) && (attValue.Length >= 1)) { switch (serializeAs) { case SettingsSerializeAs.String: { TypeConverter converter = TypeDescriptor.GetConverter(type); if (((converter == null) || !converter.CanConvertTo(typeof(string))) || !converter.CanConvertFrom(typeof(string))) { throw new ArgumentException(System.SR.GetString("Unable_to_convert_type_from_string", new object[] { type.ToString() }), "type"); } return converter.ConvertFromInvariantString(attValue); } case SettingsSerializeAs.Xml: goto Label_0078; case SettingsSerializeAs.Binary: { byte[] buffer = Convert.FromBase64String(attValue); MemoryStream serializationStream = null; try { serializationStream = new MemoryStream(buffer); return new BinaryFormatter().Deserialize(serializationStream); } finally { if (serializationStream != null) { serializationStream.Close(); } } goto Label_0078; } } } return null; Label_0078: reader = new StringReader(attValue); XmlSerializer serializer = new XmlSerializer(type); return serializer.Deserialize(reader); }
private Object GetNonLazyLoadedProperty( String propertyName, SettingsSerializeAs serializeAs) { if ( (profileProperties != null) && (profileProperties.Rows.Count > 0) ) { switch (serializeAs) { case SettingsSerializeAs.String: foreach (DataRow row in profileProperties.Rows) { if (row["PropertyName"].ToString() == propertyName) { //return row["PropertyValueString"].ToString(CultureInfo.InvariantCulture); return row["PropertyValueString"]; } } break; default: foreach (DataRow row in profileProperties.Rows) { if (row["PropertyName"].ToString() == propertyName) { return row["PropertyValueBinary"]; } } break; } } return null; }
private void UpdatePropertyLocalInstance( String propertyName, Object propertyValue, SettingsSerializeAs serializeAs) { if ( (this.profileProperties != null) &&(nonLazyLoadedProfilePropertiesLoaded) ) { foreach (DataRow row in this.profileProperties.Rows) { if (row["PropertyName"].ToString() == propertyName) { switch (serializeAs) { case SettingsSerializeAs.String: row["PropertyValueString"] = propertyValue.ToString(); break; default: row["PropertyValueBinary"] = propertyValue; break; } return; } } } }
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.ConvertToString(propValue); throw new ArgumentException("Unable to convert type " + type.ToString() + " to string", "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; }
void CreateSettingsProperty(PropertyInfo prop, SettingsPropertyCollection properties, ref SettingsProvider local_provider) { SettingsAttributeDictionary dict = new SettingsAttributeDictionary(); SettingsProvider provider = null; object defaultValue = null; SettingsSerializeAs serializeAs = SettingsSerializeAs.String; bool explicitSerializeAs = false; foreach (Attribute a in prop.GetCustomAttributes(false)) { /* the attributes we handle natively here */ if (a is SettingsProviderAttribute) { var providerTypeName = ((SettingsProviderAttribute)a).ProviderTypeName; Type provider_type = Type.GetType(providerTypeName); if (provider_type == null) // Type failed to find the type by name { var typeNameParts = providerTypeName.Split('.'); if (typeNameParts.Length > 1) //Load the assembly that providerTypeName claims { var assy = Assembly.Load(typeNameParts[0]); if (assy != null) { provider_type = assy.GetType(providerTypeName); //try to get the type from that Assembly } } } provider = (SettingsProvider)Activator.CreateInstance(provider_type); provider.Initialize(null, null); } else if (a is DefaultSettingValueAttribute) { defaultValue = ((DefaultSettingValueAttribute)a).Value; } else if (a is SettingsSerializeAsAttribute) { serializeAs = ((SettingsSerializeAsAttribute)a).SerializeAs; explicitSerializeAs = true; } else if (a is ApplicationScopedSettingAttribute || a is UserScopedSettingAttribute) { dict.Add(a.GetType(), a); } else { dict.Add(a.GetType(), a); } } if (!explicitSerializeAs) { // DefaultValue is a string and if we can't convert from string to the // property type then the only other option left is for the string to // be XML. // TypeConverter converter = TypeDescriptor.GetConverter(prop.PropertyType); if (converter != null && (!converter.CanConvertFrom(typeof(string)) || !converter.CanConvertTo(typeof(string)))) { serializeAs = SettingsSerializeAs.Xml; } } SettingsProperty setting = new SettingsProperty(prop.Name, prop.PropertyType, provider, false /* XXX */, defaultValue /* XXX always a string? */, serializeAs, dict, false, false); if (providerService != null) { setting.Provider = providerService.GetSettingsProvider(setting); } if (provider == null) { if (local_provider == null) { local_provider = new LocalFileSettingsProvider() as SettingsProvider; local_provider.Initialize(null, null); } setting.Provider = local_provider; // .NET ends up to set this to providers. provider = local_provider; } if (provider != null) { /* make sure we're using the same instance of a * given provider across multiple properties */ SettingsProvider p = Providers[provider.Name]; if (p != null) { setting.Provider = p; } } properties.Add(setting); if (setting.Provider != null && Providers [setting.Provider.Name] == null) { Providers.Add(setting.Provider); } }
private static object GetObjectFromString(Type type, SettingsSerializeAs serializeAs, string attValue) { // Deal with string types if (type == typeof(string) && (string.IsNullOrEmpty(attValue) || serializeAs == SettingsSerializeAs.String)) return attValue; // Return null if there is nothing to convert if (string.IsNullOrEmpty(attValue)) 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.ConvertFromString(attValue); throw new ArgumentException("Unable to convert type: " + type.ToString() + " from string", "type"); default: return null; } }
internal static string GetElementValue(XmlNode xmlNode, SettingsSerializeAs serializeAs) { return serializeAs == SettingsSerializeAs.Xml ? xmlNode.InnerXml : xmlNode.InnerText; }
public SettingElement(string name, SettingsSerializeAs serializeAs) { }
/// <devdoc> /// Constructor takes a SettingsSerializeAs enum value. /// </devdoc> public SettingsSerializeAsAttribute(SettingsSerializeAs serializeAs) { _serializeAs = serializeAs; }
public SettingElement(String name, SettingsSerializeAs serializeAs) : this() { Name = name; SerializeAs = serializeAs; }