|
|
@ -1,5 +1,6 @@
|
|
|
|
using System;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
using System.Reflection;
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Marr.Data.Reflection
|
|
|
|
namespace Marr.Data.Reflection
|
|
|
@ -8,6 +9,8 @@ namespace Marr.Data.Reflection
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
private static readonly Dictionary<string, MemberInfo> MemberCache = new Dictionary<string, MemberInfo>();
|
|
|
|
private static readonly Dictionary<string, MemberInfo> MemberCache = new Dictionary<string, MemberInfo>();
|
|
|
|
|
|
|
|
private static readonly Dictionary<string, GetterDelegate> GetterCache = new Dictionary<string, GetterDelegate>();
|
|
|
|
|
|
|
|
private static readonly Dictionary<string, SetterDelegate> SetterCache = new Dictionary<string, SetterDelegate>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static MemberInfo GetMember(Type entityType, string name)
|
|
|
|
private static MemberInfo GetMember(Type entityType, string name)
|
|
|
@ -24,80 +27,119 @@ namespace Marr.Data.Reflection
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Sets an entity field value by name to the passed in 'val'.
|
|
|
|
/// Gets an entity field value by name.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
public void SetFieldValue<T>(T entity, string fieldName, object val)
|
|
|
|
public object GetFieldValue(object entity, string fieldName)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var member = GetMember(entity.GetType(), fieldName);
|
|
|
|
var member = GetMember(entity.GetType(), fieldName);
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// Handle DB null values
|
|
|
|
|
|
|
|
if (val == DBNull.Value)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (member.MemberType == MemberTypes.Field)
|
|
|
|
if (member.MemberType == MemberTypes.Field)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
(member as FieldInfo).SetValue(entity,
|
|
|
|
return (member as FieldInfo).GetValue(entity);
|
|
|
|
ReflectionHelper.GetDefaultValue((member as FieldInfo).FieldType));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (member.MemberType == MemberTypes.Property)
|
|
|
|
if (member.MemberType == MemberTypes.Property)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var pi = (member as PropertyInfo);
|
|
|
|
return BuildGetter(entity.GetType(), fieldName)(entity);
|
|
|
|
if (pi.CanWrite)
|
|
|
|
|
|
|
|
(member as PropertyInfo).SetValue(entity,
|
|
|
|
|
|
|
|
ReflectionHelper.GetDefaultValue(
|
|
|
|
|
|
|
|
(member as PropertyInfo).PropertyType), null);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new DataMappingException(string.Format("The DataMapper could not get the value for {0}.{1}.", entity.GetType().Name, fieldName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// Instantiates a type using the FastReflector library for increased speed.
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <param name="type"></param>
|
|
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
|
|
public object CreateInstance(Type type)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (member.MemberType == MemberTypes.Field)
|
|
|
|
return Activator.CreateInstance(type);
|
|
|
|
(member as FieldInfo).SetValue(entity, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else if (member.MemberType == MemberTypes.Property)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public GetterDelegate BuildGetter(Type type, string memberName)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var pi = (member as PropertyInfo);
|
|
|
|
GetterDelegate getter;
|
|
|
|
if (pi.CanWrite)
|
|
|
|
var key = type.FullName + memberName;
|
|
|
|
(member as PropertyInfo).SetValue(entity, val, null);
|
|
|
|
if (!GetterCache.TryGetValue(key, out getter))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
getter = GetPropertyGetter((PropertyInfo)GetMember(type, memberName));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return getter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public SetterDelegate BuildSetter(Type type, string memberName)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
SetterDelegate setter;
|
|
|
|
|
|
|
|
var key = type.FullName + memberName;
|
|
|
|
|
|
|
|
if (!SetterCache.TryGetValue(key, out setter))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
setter = GetPropertySetter((PropertyInfo)GetMember(type, memberName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return setter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static SetterDelegate GetPropertySetter(PropertyInfo propertyInfo)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
string msg = string.Format("The DataMapper was unable to load the following field: {0}. \nDetails: {1}", fieldName, ex.Message);
|
|
|
|
var propertySetMethod = propertyInfo.GetSetMethod();
|
|
|
|
throw new DataMappingException(msg, ex);
|
|
|
|
if (propertySetMethod == null) return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if NO_EXPRESSIONS
|
|
|
|
|
|
|
|
return (o, convertedValue) =>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
propertySetMethod.Invoke(o, new[] { convertedValue });
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
var instance = Expression.Parameter(typeof(object), "i");
|
|
|
|
|
|
|
|
var argument = Expression.Parameter(typeof(object), "a");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var instanceParam = Expression.Convert(instance, propertyInfo.DeclaringType);
|
|
|
|
|
|
|
|
var valueParam = Expression.Convert(argument, propertyInfo.PropertyType);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var setterCall = Expression.Call(instanceParam, propertyInfo.GetSetMethod(), valueParam);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Expression.Lambda<SetterDelegate>(setterCall, instance, argument).Compile();
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
private static GetterDelegate GetPropertyGetter(PropertyInfo propertyInfo)
|
|
|
|
/// Gets an entity field value by name.
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
public object GetFieldValue(object entity, string fieldName)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var member = GetMember(entity.GetType(), fieldName);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (member.MemberType == MemberTypes.Field)
|
|
|
|
var getMethodInfo = propertyInfo.GetGetMethod();
|
|
|
|
|
|
|
|
if (getMethodInfo == null) return null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if NO_EXPRESSIONS
|
|
|
|
|
|
|
|
return o => propertyInfo.GetGetMethod().Invoke(o, new object[] { });
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return (member as FieldInfo).GetValue(entity);
|
|
|
|
var oInstanceParam = Expression.Parameter(typeof(object), "oInstanceParam");
|
|
|
|
|
|
|
|
var instanceParam = Expression.Convert(oInstanceParam, propertyInfo.DeclaringType);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var exprCallPropertyGetFn = Expression.Call(instanceParam, getMethodInfo);
|
|
|
|
|
|
|
|
var oExprCallPropertyGetFn = Expression.Convert(exprCallPropertyGetFn, typeof(object));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var propertyGetFn = Expression.Lambda<GetterDelegate>
|
|
|
|
|
|
|
|
(
|
|
|
|
|
|
|
|
oExprCallPropertyGetFn,
|
|
|
|
|
|
|
|
oInstanceParam
|
|
|
|
|
|
|
|
).Compile();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return propertyGetFn;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (member.MemberType == MemberTypes.Property)
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if ((member as PropertyInfo).CanRead)
|
|
|
|
Console.Write(ex.Message);
|
|
|
|
return (member as PropertyInfo).GetValue(entity, null);
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new DataMappingException(string.Format("The DataMapper could not get the value for {0}.{1}.", entity.GetType().Name, fieldName));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// Instantiantes a type using the FastReflector library for increased speed.
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <param name="type"></param>
|
|
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
|
|
public object CreateInstance(Type type)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return Activator.CreateInstance(type);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|