|
|
@ -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)
|
|
|
@ -23,54 +26,6 @@ namespace Marr.Data.Reflection
|
|
|
|
return member;
|
|
|
|
return member;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// Sets an entity field value by name to the passed in 'val'.
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
public void SetFieldValue<T>(T entity, string fieldName, object val)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var member = GetMember(entity.GetType(), fieldName);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// Handle DB null values
|
|
|
|
|
|
|
|
if (val == DBNull.Value)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (member.MemberType == MemberTypes.Field)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
(member as FieldInfo).SetValue(entity,
|
|
|
|
|
|
|
|
ReflectionHelper.GetDefaultValue((member as FieldInfo).FieldType));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (member.MemberType == MemberTypes.Property)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var pi = (member as PropertyInfo);
|
|
|
|
|
|
|
|
if (pi.CanWrite)
|
|
|
|
|
|
|
|
(member as PropertyInfo).SetValue(entity,
|
|
|
|
|
|
|
|
ReflectionHelper.GetDefaultValue(
|
|
|
|
|
|
|
|
(member as PropertyInfo).PropertyType), null);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (member.MemberType == MemberTypes.Field)
|
|
|
|
|
|
|
|
(member as FieldInfo).SetValue(entity, val);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else if (member.MemberType == MemberTypes.Property)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var pi = (member as PropertyInfo);
|
|
|
|
|
|
|
|
if (pi.CanWrite)
|
|
|
|
|
|
|
|
(member as PropertyInfo).SetValue(entity, val, null);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
|
|
|
/// <summary>
|
|
|
|
/// Gets an entity field value by name.
|
|
|
|
/// Gets an entity field value by name.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
@ -84,14 +39,14 @@ namespace Marr.Data.Reflection
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (member.MemberType == MemberTypes.Property)
|
|
|
|
if (member.MemberType == MemberTypes.Property)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if ((member as PropertyInfo).CanRead)
|
|
|
|
return BuildGetter(entity.GetType(), fieldName)(entity);
|
|
|
|
return (member as PropertyInfo).GetValue(entity, null);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new DataMappingException(string.Format("The DataMapper could not get the value for {0}.{1}.", entity.GetType().Name, fieldName));
|
|
|
|
throw new DataMappingException(string.Format("The DataMapper could not get the value for {0}.{1}.", entity.GetType().Name, fieldName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// Instantiantes a type using the FastReflector library for increased speed.
|
|
|
|
/// Instantiates a type using the FastReflector library for increased speed.
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="type"></param>
|
|
|
|
/// <param name="type"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <returns></returns>
|
|
|
@ -99,5 +54,92 @@ namespace Marr.Data.Reflection
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return Activator.CreateInstance(type);
|
|
|
|
return Activator.CreateInstance(type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public GetterDelegate BuildGetter(Type type, string memberName)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
GetterDelegate getter;
|
|
|
|
|
|
|
|
var key = type.FullName + memberName;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static SetterDelegate GetPropertySetter(PropertyInfo propertyInfo)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var propertySetMethod = propertyInfo.GetSetMethod();
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static GetterDelegate GetPropertyGetter(PropertyInfo propertyInfo)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var getMethodInfo = propertyInfo.GetGetMethod();
|
|
|
|
|
|
|
|
if (getMethodInfo == null) return null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if NO_EXPRESSIONS
|
|
|
|
|
|
|
|
return o => propertyInfo.GetGetMethod().Invoke(o, new object[] { });
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Console.Write(ex.Message);
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|