Facade cleanup and refactoring

This commit is contained in:
Stephan
2017-09-26 14:57:50 +02:00
parent 91b8f073ad
commit c24fdc0ddf
53 changed files with 831 additions and 757 deletions

View File

@@ -12,35 +12,6 @@ namespace Umbraco.Core
/// </summary>
public static class ReflectionUtilities
{
private static Func<TInstance, TValue> GetPropertyGetter<TInstance, TValue>(PropertyInfo property)
{
var type = typeof(TInstance);
var getMethod = property.GetMethod;
if (getMethod == null)
throw new InvalidOperationException($"Property {type}.{property.Name} : {property.PropertyType} does not have a getter.");
var exprThis = Expression.Parameter(type, "this");
var exprCall = Expression.Call(exprThis, getMethod);
var expr = Expression.Lambda<Func<TInstance, TValue>>(exprCall, exprThis);
return expr.CompileToDelegate();
}
private static Action<TInstance, TValue> GetPropertySetter<TInstance, TValue>(PropertyInfo property)
{
var type = typeof(TInstance);
var setMethod = property.SetMethod;
if (setMethod == null)
throw new InvalidOperationException($"Property {type}.{property.Name} : {property.PropertyType} does not have a setter.");
var exprThis = Expression.Parameter(type, "this");
var exprArg0 = Expression.Parameter(typeof(TValue), "value");
var exprCall = Expression.Call(exprThis, setMethod, exprArg0);
var expr = Expression.Lambda<Action<TInstance, TValue>>(exprCall, exprThis, exprArg0);
return expr.CompileToDelegate();
}
public static Func<TInstance, TValue> GetPropertyGetter<TInstance, TValue>(string propertyName)
{
var type = typeof(TInstance);
@@ -88,6 +59,20 @@ namespace Umbraco.Core
return expr.CompileToDelegate();
}
public static Func<object> GetCtor(Type type)
{
// get the constructor infos
var ctor = type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
null, Type.EmptyTypes, null);
if (ctor == null)
throw new InvalidOperationException($"Could not find constructor {type}.ctor().");
var exprNew = Expression.New(ctor);
var expr = Expression.Lambda<Func<object>>(exprNew);
return expr.CompileToDelegate();
}
public static Func<TArg0, TInstance> GetCtor<TInstance, TArg0>()
{
var type = typeof(TInstance);
@@ -155,6 +140,35 @@ namespace Umbraco.Core
return GetMethod<TMethod>(method, methodName, type, parameterTypes, returnType);
}
private static Func<TInstance, TValue> GetPropertyGetter<TInstance, TValue>(PropertyInfo property)
{
var type = typeof(TInstance);
var getMethod = property.GetMethod;
if (getMethod == null)
throw new InvalidOperationException($"Property {type}.{property.Name} : {property.PropertyType} does not have a getter.");
var exprThis = Expression.Parameter(type, "this");
var exprCall = Expression.Call(exprThis, getMethod);
var expr = Expression.Lambda<Func<TInstance, TValue>>(exprCall, exprThis);
return expr.CompileToDelegate();
}
private static Action<TInstance, TValue> GetPropertySetter<TInstance, TValue>(PropertyInfo property)
{
var type = typeof(TInstance);
var setMethod = property.SetMethod;
if (setMethod == null)
throw new InvalidOperationException($"Property {type}.{property.Name} : {property.PropertyType} does not have a setter.");
var exprThis = Expression.Parameter(type, "this");
var exprArg0 = Expression.Parameter(typeof(TValue), "value");
var exprCall = Expression.Call(exprThis, setMethod, exprArg0);
var expr = Expression.Lambda<Action<TInstance, TValue>>(exprCall, exprThis, exprArg0);
return expr.CompileToDelegate();
}
private static void GetMethodParms<TMethod>(out Type[] parameterTypes, out Type returnType)
{
var typeM = typeof(TMethod);
@@ -260,6 +274,9 @@ namespace Umbraco.Core
return expr.Compile();
}
// not sure we want this at all?
/*
public static object GetStaticProperty(this Type type, string propertyName, Func<IEnumerable<PropertyInfo>, PropertyInfo> filter = null)
{
var propertyInfo = GetPropertyInfo(type, propertyName, filter);
@@ -418,6 +435,7 @@ namespace Umbraco.Core
propInfo.SetValue(obj, val, null);
}
*/
public static Action CompileToDelegate(Expression<Action> expr)
{