Just a test questionJust a test questionJust a test questionJust a test questionJust a test questionJust a test questionJust a test questionJust a test questionJust a test questionJust a test questionJust a test questionJust a test questionJust a test questionJust a test questionJust a test questionJust a test questionJust a test questionJust a test questionJust a test questionJust a test questionJust a test questionJust a test question
protected virtual object InnerConvertValue(object value_, Type destinationType_, out TypeConversionException exception_)
{
exception_ = null;
TypeConversionEventArgs args = new TypeConversionEventArgs(value_, destinationType_);
OnBeforeDefaultConversion(args);
bool handled = args.Handled;
bool successful = false;
if (!handled)
{
if (null != value_ && destinationType_.IsAssignableFrom(value_.GetType()) && destinationType_ != typeof(object))
{
args.ConvertedValue = value_;
handled = true;
successful = true;
}
}
if (!handled)
{
if (null == value_ && destinationType_.IsValueType && !TypeReflector.IsNullableValueType(destinationType_))
{
exception_ = new TypeConversionException("Cannot convert a null to a value type");
successful = false;
handled = true;
}
else if (TypeReflector.IsNullableValueType(destinationType_))
{
if (null == value_)
{
args.ConvertedValue = null;
handled = true;
successful = true;
}
else
{
args.ConvertedValue = InnerConvertValue(value_, Nullable.GetUnderlyingType(destinationType_), out exception_);
handled = true;
successful = true;
}
}
else if (null == value_ && !destinationType_.IsValueType)
{
args.ConvertedValue = null;
handled = true;
successful = true;
}
}
Type sourceType = null;
if (null != value_)
{
sourceType = value_.GetType();
}
if (!handled)
{
if (RegisteredConverters.CanConvert(value_, destinationType_))
{
args.ConvertedValue = RegisteredConverters.ConvertValue(value_, destinationType_);
handled = true;
successful = true;
}
}
if (!handled && null != sourceType)
{
MethodInfo convMethod = FindConversionOperator(sourceType, destinationType_, true);
if (null != convMethod)
{
try
{
args.ConvertedValue = convMethod.Invoke(null, new object[] { value_ });
successful = true;
}
catch (TargetInvocationException ex)
{
exception_ = new TypeConversionException(string.Format("Error invoking conversion operator from {0} to {1}", sourceType.Name, destinationType_.Name), ex.InnerException);
successful = false;
}
handled = true;
}
}
if (!handled)
{
if (destinationType_.IsValueType && sourceType == typeof(string))
{
string stringValue = (string)value_;
if (destinationType_.IsEnum)
{
args.ConvertedValue = Enum.Parse(destinationType_, stringValue);
handled = true;
successful = true;
}
else if (destinationType_ == typeof(Int32))
{
int result;
if (Int32.TryParse(stringValue, out result))
{
args.ConvertedValue = result;
handled = true;
successful = true;
}
}
else if (destinationType_ == typeof(bool))
{
if (stringValue.ToLower().Trim() == "true")
{
args.ConvertedValue = true;
handled = true;
successful = true;
}
else if (stringValue.ToLower().Trim() == "false")
{
args.ConvertedValue = false;
handled = true;
successful = true;
}
}
else if (destinationType_ == typeof(Guid))
{
args.ConvertedValue = new Guid(stringValue);
handled = true;
successful = true;
}
else if (destinationType_ == typeof(DateTime))
{
DateTime result;
if (DateTime.TryParse(stringValue, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AllowWhiteSpaces, out result))
{
args.ConvertedValue = result;
handled = true;
successful = true;
}
}
else if (destinationType_ == typeof(Int64))
{
Int64 result;
if (Int64.TryParse(stringValue, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out result))
{
args.ConvertedValue = result;
handled = true;
successful = true;
}
}
else if (destinationType_ == typeof(Int16))
{
Int16 result;
if (Int16.TryParse(stringValue, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out result))
{
args.ConvertedValue = result;
handled = true;
successful = true;
}
}
else if (destinationType_ == typeof(Decimal))
{
Decimal result;
if (Decimal.TryParse(stringValue, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out result))
{
args.ConvertedValue = result;
handled = true;
successful = true;
}
}
else if (destinationType_ == typeof(Single))
{
Single result;
if (Single.TryParse(stringValue, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out result))
{
args.ConvertedValue = result;
handled = true;
successful = true;
}
}
else if (destinationType_ == typeof(Double))
{
Double result;
if (Double.TryParse(stringValue, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out result))
{
args.ConvertedValue = result;
handled = true;
successful = true;
}
}
else if (destinationType_ == typeof(Byte))
{
Byte result;
if (Byte.TryParse(stringValue, out result))
{
args.ConvertedValue = result;
handled = true;
successful = true;
}
}
}
}
if (!handled && sourceType == typeof(int) && destinationType_ == typeof(decimal))
{
args.ConvertedValue = (decimal)(int)value_;
handled = true;
successful = true;
}
if (!handled && sourceType == typeof(int) && destinationType_ == typeof(long))
{
args.ConvertedValue = (long)(int)value_;
handled = true;
successful = true;
}
if (!handled && sourceType == typeof(int) && destinationType_ == typeof(uint) && ((int)value_) >= 0)
{
args.ConvertedValue = (uint)(int)value_;
handled = true;
successful = true;
}
if (!handled && sourceType == typeof(int) && destinationType_ == typeof(ulong) && ((int)value_) >= 0)
{
args.ConvertedValue = (ulong)(int)value_;
handled = true;
successful = true;
}
if (!handled && sourceType == typeof(long) && destinationType_ == typeof(decimal))
{
args.ConvertedValue = (decimal)(long)value_;
handled = true;
successful = true;
}
if (!handled && sourceType == typeof(long) && destinationType_ == typeof(int) && ((long)value_) <= Int32.MaxValue)
{
args.ConvertedValue = (int)(long)value_;
handled = true;
successful = true;
}
if (!handled && sourceType == typeof(long) && destinationType_ == typeof(uint) && ((long)value_) >= 0 && ((long)value_) <= UInt32.MaxValue)
{
args.ConvertedValue = (uint)(long)value_;
handled = true;
successful = true;
}
if (!handled && sourceType == typeof(long) && destinationType_ == typeof(ulong) && ((long)value_) >= 0)
{
args.ConvertedValue = (ulong)(long)value_;
handled = true;
successful = true;
}
if (!handled)
{ // try type converters for source and destination types
TypeConverter sourceConverter = TypeDescriptor.GetConverter(sourceType);
TypeConverter destinatonConverter = TypeDescriptor.GetConverter(destinationType_);
if (null != sourceConverter && sourceConverter.CanConvertTo(destinationType_))
{
try
{
args.ConvertedValue = sourceConverter.ConvertTo(value_, destinationType_);
handled = true;
successful = true;
}
catch
{
handled = false;
successful = false;
}
}
else if (null != destinatonConverter && destinatonConverter.CanConvertFrom(sourceType))
{
try
{
args.ConvertedValue = destinatonConverter.ConvertFrom(value_);
handled = true;
successful = true;
}
catch
{
handled = false;
successful = false;
}
}
}
if (successful)
{
OnAfterSuccessfulDefaultConversion(args);
}
else
{
OnFailedConversion(args);
handled = args.Handled;
}
if (!handled)
{
exception_ = new TypeConversionException(value_, destinationType_);
return null;
}
else
{
return args.ConvertedValue;
}
}
Aucun commentaire:
Enregistrer un commentaire