Fix issue #25 for bad source returning unexpected cap value types.

This commit is contained in:
soukoku 2014-11-28 21:36:37 -05:00
parent df80b86f71
commit f29443a8a4

View File

@ -58,11 +58,12 @@ namespace NTwain.Data
// standard int values // standard int values
if (returnType.IsEnum) if (returnType.IsEnum)
{ {
var rawType = Enum.GetUnderlyingType(returnType);
if (tryUpperWord) if (tryUpperWord)
{ {
// small routine to work with bad sources that may put // small routine to work with bad sources that may put
// 16bit value in the upper word instead of lower word (as per the twain spec). // 16bit value in the upper word instead of lower word (as per the twain spec).
var rawType = Enum.GetUnderlyingType(returnType);
if (typeof(ushort).IsAssignableFrom(rawType)) if (typeof(ushort).IsAssignableFrom(rawType))
{ {
var intVal = Convert.ToUInt32(value, CultureInfo.InvariantCulture); var intVal = Convert.ToUInt32(value, CultureInfo.InvariantCulture);
@ -73,10 +74,12 @@ namespace NTwain.Data
} }
} }
} }
// this may work better? // old method:
return (TEnum)Enum.ToObject(returnType, value); // return (TEnum)Enum.ToObject(returnType, value);
//// cast to underlying type first then to the enum
//return (T)Convert.ChangeType(value, rawType); // new method:
// try to convert to enum's underlying type first then cast to the enum
return (TEnum)Convert.ChangeType(value, rawType);
} }
else if (typeof(IConvertible).IsAssignableFrom(returnType)) else if (typeof(IConvertible).IsAssignableFrom(returnType))
{ {