Habanero.Base.DateTimeUtcNowConverter.ConvertTo C# (CSharp) Method

ConvertTo() public method

Converts the given value object to the specified type, using the specified context and culture information.
The parameter is null. /// The conversion cannot be performed. ///
public ConvertTo ( ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType ) : object
context ITypeDescriptorContext An that provides a format context. ///
culture System.Globalization.CultureInfo A . If null is passed, the current culture is assumed. ///
value object The to convert. ///
destinationType System.Type The to convert the parameter to. ///
return object
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(DateTime)) return DateTimeUtcNow.Value;
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

Usage Example

 public void Test_ConvertTo_WithDateTime_ShouldReturnNowValue()
 {
     //---------------Set up test pack-------------------
     DateTimeUtcNowConverter dateTimeUtcNowConverter = new DateTimeUtcNowConverter();
     DateTimeUtcNow dateTimeUtcNow = new DateTimeUtcNow();
     DateTime snapshot = DateTime.UtcNow;
     //---------------Assert Precondition----------------
     //---------------Execute Test ----------------------
     object result = dateTimeUtcNowConverter.ConvertTo(dateTimeUtcNow, typeof(DateTime));
     //---------------Test Result -----------------------
     DateTime dateTime = TestUtil.AssertIsInstanceOf<DateTime>(result);
     Assert.Greater(dateTime, snapshot.AddSeconds(-1));
     Assert.Less(dateTime, snapshot.AddSeconds(1));
     Assert.AreEqual(dateTime.Kind, DateTimeKind.Utc);
 }
DateTimeUtcNowConverter