/// <summary> /// Saves all the extended settings into a simple multi-line key/value pair. /// This also handles the case for stringclobs. /// </summary> /// <param name="objToSave">The obj to save.</param> /// <param name="includeProps">The include props.</param> /// <param name="excludeProps">The exclude props.</param> /// <param name="stringClobProps">The string clob props.</param> /// <returns></returns> public static string CreateState(object objToSave, string includeProps, string excludeProps, string stringClobProps) { if (objToSave == null) { throw new ArgumentNullException("Object to save is null"); } if (string.IsNullOrEmpty(includeProps) && string.IsNullOrEmpty(includeProps) && string.IsNullOrEmpty(includeProps)) { return(string.Empty); } SimpleStateMap propStateMap = null; if (_stateMap.ContainsKey(objToSave.GetType().FullName)) { propStateMap = _stateMap[objToSave.GetType().FullName]; } else { propStateMap = SimpleStateMap.ToPropStateMap(objToSave.GetType(), includeProps, excludeProps, stringClobProps); _stateMap[objToSave.GetType().FullName] = propStateMap; } // 4. Now save state for each property. var buffer = new StringBuilder(); foreach (var propPair in propStateMap.IncludeProps) { // Save state for each property. PropertyInfo prop = propStateMap.AllPropMap[propPair.Key]; object val = prop.GetValue(objToSave, null); bool isStringClob = propStateMap.StringClobProps.ContainsKey(prop.Name); string valString = string.Empty; if (isStringClob && val != null) { valString = ((string)val).Replace(Environment.NewLine, "<br />"); } else if (val != null) { valString = val.ToString(); } string state = string.Format("{0},{1}" + Environment.NewLine, prop.Name, valString); buffer.Append(state); } string completeState = buffer.ToString(); return(completeState); }
/// <summary> /// Load the state into the properties of <paramref name="objToSave"/> /// </summary> /// <param name="objToSave">The obj to save.</param> /// <param name="state">The state as a string. /// e.g. /// line #: propertyname,value /// line1 : Title,Learn how to program in asp.net mvc./r/n /// line2 : Cost,30/r/n /// line3 : Content,learn how to program in asp.net using commonlibrary.net CMS which is an /// : ASP.NET MVC CMS using commonlibrary.net/r/n /// line6 : Url,1,http://commonlibrarynetcms.codeplex.com/r/n /// </param> /// <param name="includeProps">The include props.</param> /// <param name="excludeProps">The exclude props.</param> /// <param name="stringClobProps">The string clob props.</param> public static void LoadState(object objToSave, string state, string includeProps, string excludeProps, string stringClobProps) { if (objToSave == null) { throw new ArgumentNullException("Object to save is null"); } if (string.IsNullOrEmpty(includeProps) && string.IsNullOrEmpty(includeProps) && string.IsNullOrEmpty(includeProps)) { return; } SimpleStateMap propStateMap = null; if (_stateMap.ContainsKey(objToSave.GetType().FullName)) { propStateMap = _stateMap[objToSave.GetType().FullName]; } else { propStateMap = SimpleStateMap.ToPropStateMap(objToSave.GetType(), includeProps, excludeProps, stringClobProps); _stateMap[objToSave.GetType().FullName] = propStateMap; } using (StringReader reader = new StringReader(state)) { string line; while ((line = reader.ReadLine()) != null) { // Format is <propertyname>,<numberoflines>,<value> int ndxComma = line.IndexOf(','); string propName = line.Substring(0, ndxComma); if (propStateMap.AllPropMap.ContainsKey(propName)) { string val = line.Substring(ndxComma + 1); // Now set the value. var prop = propStateMap.AllPropMap[propName]; object convertedVal = Converter.ConvertTo(prop.PropertyType, val); prop.SetValue(objToSave, convertedVal, null); } } } }