mirror of
https://github.com/Readarr/Readarr.git
synced 2026-04-26 22:46:37 -04:00
Moved source code under src folder - massive change
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
|
||||
namespace Marr.Data.Reflection
|
||||
{
|
||||
public interface IReflectionStrategy
|
||||
{
|
||||
object GetFieldValue(object entity, string fieldName);
|
||||
|
||||
GetterDelegate BuildGetter(Type type, string memberName);
|
||||
SetterDelegate BuildSetter(Type type, string memberName);
|
||||
|
||||
object CreateInstance(Type type);
|
||||
}
|
||||
|
||||
public delegate void SetterDelegate(object instance, object value);
|
||||
public delegate object GetterDelegate(object instance);
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/* Copyright (C) 2008 - 2011 Jordan Marr
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 3 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Marr.Data.Reflection
|
||||
{
|
||||
public class ReflectionHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a DBNull.Value to a null for a reference field,
|
||||
/// or the default value of a value field.
|
||||
/// </summary>
|
||||
/// <param name="fieldType"></param>
|
||||
/// <returns></returns>
|
||||
public static object GetDefaultValue(Type fieldType)
|
||||
{
|
||||
if (fieldType.IsGenericType)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (fieldType.IsValueType)
|
||||
{
|
||||
return Activator.CreateInstance(fieldType);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the CLR data type of a MemberInfo.
|
||||
/// If the type is nullable, returns the underlying type.
|
||||
/// </summary>
|
||||
/// <param name="member"></param>
|
||||
/// <returns></returns>
|
||||
public static Type GetMemberType(MemberInfo member)
|
||||
{
|
||||
Type memberType = null;
|
||||
if (member.MemberType == MemberTypes.Property)
|
||||
memberType = (member as PropertyInfo).PropertyType;
|
||||
else if (member.MemberType == MemberTypes.Field)
|
||||
memberType = (member as FieldInfo).FieldType;
|
||||
else
|
||||
memberType = typeof(object);
|
||||
|
||||
// Handle nullable types - get underlying type
|
||||
if (memberType.IsGenericType && memberType.GetGenericTypeDefinition() == typeof(Nullable<>))
|
||||
{
|
||||
memberType = memberType.GetGenericArguments()[0];
|
||||
}
|
||||
|
||||
return memberType;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Marr.Data.Reflection
|
||||
{
|
||||
public class SimpleReflectionStrategy : IReflectionStrategy
|
||||
{
|
||||
|
||||
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)
|
||||
{
|
||||
MemberInfo member;
|
||||
var key = entityType.FullName + name;
|
||||
if (!MemberCache.TryGetValue(key, out member))
|
||||
{
|
||||
member = entityType.GetMember(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)[0];
|
||||
MemberCache[key] = member;
|
||||
}
|
||||
|
||||
return member;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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)
|
||||
{
|
||||
return (member as FieldInfo).GetValue(entity);
|
||||
}
|
||||
if (member.MemberType == MemberTypes.Property)
|
||||
{
|
||||
return BuildGetter(entity.GetType(), fieldName)(entity);
|
||||
}
|
||||
throw new DataMappingException(string.Format("The DataMapper could not get the value for {0}.{1}.", entity.GetType().Name, fieldName));
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Instantiates 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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user