/// <summary> /// Prepares the value to be converted to a format appropriate for sql /// </summary> /// <param name="objValue">The value to prepare</param> /// <returns>Returns the reformatted object</returns> public virtual object PrepareValue(object objValue) { if (objValue is Guid) { if (Guid.Empty.Equals(objValue)) { return(DBNull.Value); } return(((Guid)objValue).ToString("B").ToUpper(CultureInfo.InvariantCulture)); } if (objValue is bool) { return((bool)objValue ? 1 : 0); } if (objValue is Image) { return(SerialisationUtilities.ObjectToByteArray(objValue)); } if (objValue is CustomProperty) { return(((CustomProperty)objValue).GetPersistValue()); } if (objValue is TimeSpan) { TimeSpan time = (TimeSpan)objValue; return(TimeSpanDataMapper.BaseDate.Add(time)); } return(objValue); }
/// <summary> /// This method provides the functionality to convert any object to the appropriate /// type for this mapper. The default behaviour only handles null values, empty /// strings and DBNull.Value and parses all three to null. This method should be /// overridden in subtypes to parse values to the type you want. /// </summary> /// <param name="valueToParse">The value to be attempted to parse</param> /// <param name="returnValue">the parsed value, if parsing was successful</param> /// <returns>True if the parsing was succesful, false if this mapper was unable to /// parse the valueToParse.</returns> public override bool TryParsePropValue(object valueToParse, out object returnValue) { if (base.TryParsePropValue(valueToParse, out returnValue)) { return(true); } if (valueToParse is Image) { returnValue = valueToParse; return(true); } if (valueToParse is byte[]) { returnValue = SerialisationUtilities.ByteArrayToObject((byte[])valueToParse); return(true); } if (valueToParse is String) { var bytes = Convert.FromBase64String((string)valueToParse); returnValue = new Bitmap(new MemoryStream(bytes)); return(true); } returnValue = null; return(false); }
public void TestTwoWayConversion() { Image image = (Image)_resourceManager.GetObject("TestJpeg"); byte[] bytesOut = SerialisationUtilities.ObjectToByteArray(image); Object objectOut = SerialisationUtilities.ByteArrayToObject(bytesOut); Assert.AreEqual(image.GetType(), objectOut.GetType()); }
/// <summary> /// Serialise an object into the specified byte stream. /// If the object cannot be serialised then the byte stream /// will be null and the nonSerialisable parameter will be false. /// </summary> /// <param name="objectToSerialise"> /// <c>object</c> to serialise. /// </param> /// <param name="serialisedData"> /// Serialised representation of the object. /// </param> /// <param name="nonSerialisable"> /// Indicates whether the object was successfully serialised or not. /// </param> private static void serialiseData(object objectToSerialise, out byte [] serialisedData, out bool nonSerialisable) { nonSerialisable = false; serialisedData = null; try { serialisedData = SerialisationUtilities.Serialise(objectToSerialise); } catch (SerializationException) { nonSerialisable = true; } }
public void TryParsePropValue_ConvertsByteArrayToImage() { //---------------Set up test pack------------------- var dataMapper = new ImageDataMapper(); var img = new System.Drawing.Bitmap(100, 100); var valueToParse = SerialisationUtilities.ObjectToByteArray(img); object parsedValue; //---------------Execute Test ---------------------- var parseSucceed = dataMapper.TryParsePropValue(valueToParse, out parsedValue); //---------------Test Result ----------------------- Assert.IsTrue(parseSucceed); Assert.IsInstanceOf(typeof(System.Drawing.Bitmap), parsedValue); Assert.AreEqual(img.Width, ((System.Drawing.Bitmap)parsedValue).Width); Assert.AreEqual(img.Height, ((System.Drawing.Bitmap)parsedValue).Height); }