/// <summary> /// Creates a result for the demanded Value /// </summary> /// <param name="name">the name of the requested field</param> /// <param name="type">the requested type of the field</param> /// <param name="result">output result of the resulting value</param> /// <returns>a value indicating whether the requested value was found</returns> private bool GetValue(string name, Type type, out object result) { result = null; if (string.IsNullOrEmpty(name)) { return(false); } if (!values.ContainsKey(name /*.ToUpper()*/)) { return(false); } object v = values[name /*.ToUpper()*/]; if (v.GetType() != type && !(v is DBNull)) { if (v.GetType() == typeof(byte[]) && typeof(Image).IsAssignableFrom(type)) { MemoryStream mst = new MemoryStream(v as byte[]); v = Image.FromStream(mst); } else if (v is DynamicResult) { v = (v as DynamicResult).GetIndexValue(); } else if (v is SmartProperty) { v = (v as SmartProperty).Value; } else if (v is Delegate) { throw new Exception(DataAccessResources .DynamicResult_GetValue_Delegates_are_not_supported_); } else { if (type != typeof(object) && !(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))) { v = TypeConverter.Convert(v, type); } else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>)) { if (v != null) { try { v = TypeConverter.Convert(v, Nullable.GetUnderlyingType(type)); } catch { LogEnvironment.LogEvent( string.Format( DataAccessResources .DynamicResult_GetValue_Can_not_convert___0___to__1__, v, type.FullName), LogSeverity.Error, "DataAccess"); v = null; } } } } } else if (v is DBNull) { v = null; } result = v; return(true); }
/// <summary> /// Sets a value of a particular column of this dynamic Result object /// </summary> /// <param name="name">the columnName</param> /// <param name="value">the new value of the column</param> /// <returns>a value indicating whether the requested column was found</returns> private bool SetValue(string name, object value) { bool retVal = values.ContainsKey(name /*.ToUpper()*/); if (retVal || Extendable) { if (!retVal) { object v = value; if (value is DynamicResult || value is SmartProperty) { v = new object(); } else if (value is Image) { v = new byte[0]; } types.Add(name /*.ToUpper()*/, v?.GetType() ?? typeof(object)); } Type t = types[name /*.ToUpper()*/]; if (value == null) { value = DBNull.Value; } else { if (!t.IsAssignableFrom(value.GetType()) && !(value is DynamicResult) && !(value is SmartProperty)) { if (value is Image && t == typeof(byte[])) { Image img = value as Image; MemoryStream mst = new MemoryStream(); img.Save(mst, ImageFormat.Jpeg); mst.Close(); value = mst.ToArray(); } else { LogEnvironment.LogDebugEvent(null, string.Format("Target-Type for {0} is: {1}", name, t.FullName), (int)LogSeverity.Report, "DataAccess"); if (value is IConvertible) { try { value = TypeConverter.Convert(value, t); } catch (Exception ex) { LogEnvironment.LogEvent( string.Format("Error while converting new value of {0} to type {1}: {2}", name, t.FullName, ex), LogSeverity.Warning, "DataAccess"); } } } } else if (value is SmartProperty) { types[name /*.ToUpper()*/] = typeof(object); } } if (!retVal || !(values[name /*.ToUpper()*/] is SmartProperty && (values[name /*.ToUpper()*/] as SmartProperty).SetterMethod != null)) { object oldValue = retVal ? values[name /*.ToUpper()*/] : null; SmartProperty smart = oldValue as SmartProperty; if (smart != null) { smart.PropertyChanged -= SmartPropertyChanged; } else { INotifyCollectionChanged notifyCollection = oldValue as INotifyCollectionChanged; if (notifyCollection != null) { notifyCollection.CollectionChanged -= ChildCollectionChanged; } } values[name /*.ToUpper()*/] = value; smart = value as SmartProperty; if (smart != null) { smart.PropertyChanged += SmartPropertyChanged; } else { INotifyCollectionChanged notifyCollection = value as INotifyCollectionChanged; if (notifyCollection != null) { notifyCollection.CollectionChanged += ChildCollectionChanged; } } if (controller != null && autoSave) { controller.Save(this); } } else { (values[name /*.ToUpper()*/] as SmartProperty).Value = value; } (from z in values where (z.Value is SmartProperty && ((SmartProperty)z.Value).MonitoredProperties == null) || z.Key.Equals(name, StringComparison.OrdinalIgnoreCase) /*.ToUpper()*/ select new PropertyChangedEventArgs(z.Key)) .ToList().ForEach(n => OnPropertyChanged(n)); } return(retVal); }