mirror of
https://github.com/soukoku/ntwain.git
synced 2026-01-02 04:17:08 +08:00
Redone the cap value read code to support all twain types (#3).
This commit is contained in:
71
NTwain/Values/ValueConverter.cs
Normal file
71
NTwain/Values/ValueConverter.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace NTwain.Values
|
||||
{
|
||||
/// <summary>
|
||||
/// Utility on converting (possibly bad) integer values to enum values.
|
||||
/// </summary>
|
||||
public static class ValueConverter
|
||||
{
|
||||
public static IList<T> CastToEnum<T>(this IList<object> list) where T : struct,IConvertible
|
||||
{
|
||||
return list.CastToEnum<T>(true);
|
||||
}
|
||||
public static IList<T> CastToEnum<T>(this IList<object> list, bool tryUpperWord) where T : struct,IConvertible
|
||||
{
|
||||
return list.Select(o => o.ConvertToEnum<T>(tryUpperWord)).ToList();
|
||||
}
|
||||
|
||||
public static T ConvertToEnum<T>(this object value) where T : struct,IConvertible
|
||||
{
|
||||
return ConvertToEnum<T>(value, true);
|
||||
}
|
||||
public static T ConvertToEnum<T>(this object value, bool tryUpperWord) where T : struct,IConvertible
|
||||
{
|
||||
var returnType = typeof(T);
|
||||
|
||||
// standard int values
|
||||
if (returnType.IsEnum)
|
||||
{
|
||||
if (tryUpperWord)
|
||||
{
|
||||
// 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).
|
||||
var rawType = Enum.GetUnderlyingType(returnType);
|
||||
if (typeof(ushort).IsAssignableFrom(rawType))
|
||||
{
|
||||
var intVal = Convert.ToUInt32(value);
|
||||
var enumVal = GetLowerWord(intVal);
|
||||
if (!Enum.IsDefined(returnType, enumVal))
|
||||
{
|
||||
return (T)Enum.ToObject(returnType, GetUpperWord(intVal));
|
||||
}
|
||||
}
|
||||
}
|
||||
// this may work better?
|
||||
return (T)Enum.ToObject(returnType, value);
|
||||
//// cast to underlying type first then to the enum
|
||||
//return (T)Convert.ChangeType(value, rawType);
|
||||
}
|
||||
else if (typeof(IConvertible).IsAssignableFrom(returnType))
|
||||
{
|
||||
// for regular integers and whatnot
|
||||
return (T)Convert.ChangeType(value, returnType);
|
||||
}
|
||||
// return as-is from cap. if caller made a mistake then there should be exceptions
|
||||
return (T)value;
|
||||
}
|
||||
|
||||
static ushort GetLowerWord(uint value)
|
||||
{
|
||||
return (ushort)(value & 0xffff);
|
||||
}
|
||||
static uint GetUpperWord(uint value)
|
||||
{
|
||||
return (ushort)(value >> 16);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user