using System;
using System.Text;
using System.Data;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections.Generic;
namespace NBear.Mapping{
/// <summary>
/// DataReaderGetterGenerator
/// </summary>
public class DataReaderGetterGenerator : IMemberGetterGenerator
{
#region IMemberGetterGenerator Members
public void GetMemberValue(CodeGenerator generator, ArgBuilder targetObject, string memberName, ref LocalBuilder memberValue)
{
MethodInfo getMethod;
bool mustBeUnBox = false;
switch (Type.GetTypeCode(memberValue.LocalType))
{
//
case TypeCode.Boolean:
getMethod = typeof(IDataRecord).GetMethod("GetBoolean");
break;
case TypeCode.Byte:
getMethod = typeof(IDataRecord).GetMethod("GetByte");
break;
case TypeCode.Char:
getMethod = typeof(IDataRecord).GetMethod("GetChar");
break;
case TypeCode.DateTime:
getMethod = typeof(IDataRecord).GetMethod("GetDateTime");
break;
case TypeCode.Decimal:
getMethod = typeof(IDataRecord).GetMethod("GetDecimal");
break;
case TypeCode.Double:
getMethod = typeof(IDataRecord).GetMethod("GetDouble");
break;
case TypeCode.Single:
getMethod = typeof(IDataRecord).GetMethod("GetFloat");
break;
case TypeCode.UInt16:
getMethod = typeof(IDataRecord).GetMethod("GetInt16");
break;
case TypeCode.Int32:
getMethod = typeof(IDataRecord).GetMethod("GetInt32");
break;
case TypeCode.Int64:
getMethod = typeof(IDataRecord).GetMethod("GetInt64");
break;
case TypeCode.String:
getMethod = typeof(IDataRecord).GetMethod("GetString");
break;
default:
getMethod = typeof(IDataRecord).GetMethod("GetValue");
mustBeUnBox = true;
break;
}
if (memberValue.LocalType == typeof(Guid))
{
getMethod = typeof(IDataRecord).GetMethod("GetGuid");
mustBeUnBox = true;
}
generator.Ldarg(targetObject);
generator.Ldarg(targetObject);
generator.Load(memberName);
generator.Call(typeof(IDataRecord).GetMethod("GetOrdinal"));
generator.Call(getMethod);
if (memberValue.LocalType.IsValueType && mustBeUnBox)
{
generator.UnboxAny(memberValue.LocalType);
}
generator.Stloc(memberValue);
}
#endregion
#region IMemberGetterGenerator Members
public bool ContainsMember(string name, Type type, object inputObject)
{
//IDataReader.GetSchemaTable DataTable
IDataReader dr = (IDataReader)inputObject;
foreach (DataRow dataRow in dr.GetSchemaTable().Rows)
{
if (dataRow.ItemArray[0].ToString() == name)
{
return true;
}
}
return false;
}
#endregion
}
}
|