Moo.Core.ValueConverter.Convert C# (CSharp) Method

Convert() private method

private Convert ( object sourceValue, Type targetType ) : object
sourceValue object
targetType System.Type
return object
        public virtual object Convert(object sourceValue, Type targetType)
        {
            Guard.CheckArgumentNotNull(targetType, "targetType");
            if (sourceValue == null)
            {
                if (targetType.IsValueType)
                {
                    if (Nullable.GetUnderlyingType(targetType) == null)
                    {
                        throw new InvalidOperationException();
                    }
                }

                return null;
            }

            Type sourceType = sourceValue.GetType();

            if (targetType.IsAssignableFrom(sourceType))
            {
                return sourceValue;
            }

            return System.Convert.ChangeType(sourceValue, targetType, CultureInfo.InvariantCulture);
        }

Usage Example

Example #1
0
 public void DoConvertTest(object value, object expected, Type expectedType)
 {
     var target = new ValueConverter();
     object actual = target.Convert(value, expectedType);
     actual.ShouldBe(expected);
 }
All Usage Examples Of Moo.Core.ValueConverter::Convert
ValueConverter