2013-03-30 21:56:34 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Reflection;
|
2013-03-31 23:40:19 +00:00
|
|
|
|
using Fasterflect;
|
2013-03-30 21:56:34 +00:00
|
|
|
|
|
|
|
|
|
namespace Marr.Data.Reflection
|
|
|
|
|
{
|
|
|
|
|
public class CachedReflectionStrategy : IReflectionStrategy
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets an entity field value by name to the passed in 'val'.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SetFieldValue<T>(T entity, string fieldName, object val)
|
|
|
|
|
{
|
|
|
|
|
MemberInfo member = entity.GetType().GetMember(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)[0];
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Handle DB null values
|
|
|
|
|
if (val == DBNull.Value)
|
|
|
|
|
{
|
|
|
|
|
if (member.MemberType == MemberTypes.Field)
|
2013-03-31 23:40:19 +00:00
|
|
|
|
{
|
|
|
|
|
entity.SetFieldValue(member.Name, ReflectionHelper.GetDefaultValue(((FieldInfo)member).FieldType));
|
|
|
|
|
}
|
2013-03-30 21:56:34 +00:00
|
|
|
|
else if (member.MemberType == MemberTypes.Property)
|
|
|
|
|
{
|
2013-03-31 23:40:19 +00:00
|
|
|
|
var pi = (PropertyInfo)member;
|
2013-03-30 21:56:34 +00:00
|
|
|
|
if (pi.CanWrite)
|
2013-03-31 23:40:19 +00:00
|
|
|
|
{
|
|
|
|
|
entity.SetPropertyValue(member.Name, ReflectionHelper.GetDefaultValue(((PropertyInfo)member).PropertyType));
|
|
|
|
|
}
|
2013-03-30 21:56:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (member.MemberType == MemberTypes.Field)
|
|
|
|
|
{
|
2013-03-31 23:40:19 +00:00
|
|
|
|
entity.SetFieldValue(member.Name, val);
|
|
|
|
|
}
|
|
|
|
|
else if (member.MemberType == MemberTypes.Property && ((PropertyInfo)member).CanWrite)
|
|
|
|
|
{
|
|
|
|
|
member.Set(entity, val);
|
2013-03-30 21:56:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
string msg = string.Format("The DataMapper was unable to load the following field: {0}. \nDetails: {1}", fieldName, ex.Message);
|
|
|
|
|
throw new DataMappingException(msg, ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets an entity field value by name.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public object GetFieldValue(object entity, string fieldName)
|
|
|
|
|
{
|
|
|
|
|
MemberInfo member = entity.GetType().GetMember(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)[0];
|
|
|
|
|
|
|
|
|
|
if (member.MemberType == MemberTypes.Field)
|
|
|
|
|
{
|
2013-03-31 23:40:19 +00:00
|
|
|
|
return entity.GetFieldValue(member.Name);
|
2013-03-30 21:56:34 +00:00
|
|
|
|
}
|
2013-03-31 23:40:19 +00:00
|
|
|
|
|
|
|
|
|
if (member.MemberType == MemberTypes.Property && ((PropertyInfo)member).CanRead)
|
2013-03-30 21:56:34 +00:00
|
|
|
|
{
|
2013-03-31 23:40:19 +00:00
|
|
|
|
return entity.GetPropertyValue(member.Name);
|
2013-03-30 21:56:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new DataMappingException(string.Format("The DataMapper could not get the value for {0}.{1}.", entity.GetType().Name, fieldName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Instantiantes a type using the FastReflector library for increased speed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="type"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public object CreateInstance(Type type)
|
|
|
|
|
{
|
2013-03-31 23:40:19 +00:00
|
|
|
|
return type.CreateInstance();
|
2013-03-30 21:56:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|