Skip to content

Commit

Permalink
net40 support
Browse files Browse the repository at this point in the history
  • Loading branch information
IharBury committed Aug 2, 2015
1 parent 8158e3d commit 03416fd
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/IharBury.Expressions/BooleanExpressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ private static LambdaExpression Combine(

LambdaExpression firstExpression = null;
Expression resultBody = null;
IReadOnlyList<ParameterExpression> resultParameters = null;
IList<ParameterExpression> resultParameters = null;
foreach (var expression in expressions)
{
if (expression == null)
Expand Down
49 changes: 49 additions & 0 deletions src/IharBury.Expressions/CompatibilityExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Reflection;

namespace IharBury.Expressions
{
internal static class CompatibilityExtensions
{
internal static Type GetBaseType(this Type type)
{
if (type == null)
throw new ArgumentNullException(nameof(type));

#if NET40
return type.BaseType;
#else
return type.GetTypeInfo().BaseType;
#endif
}

internal static IEnumerable<PropertyInfo> GetDeclaredProperties(this Type type)
{
if (type == null)
throw new ArgumentNullException(nameof(type));

#if NET40
return (IEnumerable<PropertyInfo>)type.GetProperties(BindingFlags.DeclaredOnly |
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.Public |
BindingFlags.NonPublic);
#else
return type.GetTypeInfo().DeclaredProperties;
#endif
}

internal static bool GetIsConstructedGenericType(this Type type)
{
if (type == null)
throw new ArgumentNullException(nameof(type));

#if NET40
return type.IsGenericType && !type.IsGenericTypeDefinition;
#else
return type.IsConstructedGenericType;
#endif
}
}
}
18 changes: 11 additions & 7 deletions src/IharBury.Expressions/ExpressionExpander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ namespace IharBury.Expressions
{
internal sealed class ExpressionExpander : ExpressionVisitor
{
private static readonly MethodInfo MethodInfoCreateDelegateMethod =
ReflectionExpressions.GetMethodInfo<MethodInfo>(methodInfo =>
methodInfo.CreateDelegate(default(Type), default(object)));

private static readonly MethodInfo MethodInfoCreateDelegateMethod;
private static readonly MethodInfo DelegateCreateDelegateMethod;

static ExpressionExpander()
{
MethodInfoCreateDelegateMethod = typeof(MethodInfo).GetMethod(
"CreateDelegate",
new[]
{
typeof(Type),
typeof(object)
});
DelegateCreateDelegateMethod = typeof(Delegate).GetMethod(
"CreateDelegate",
new[]
Expand Down Expand Up @@ -65,7 +69,7 @@ private static bool IsCompileMethod(MethodInfo method)
throw new ArgumentNullException(nameof(method));

return (method.DeclaringType != null) &&
method.DeclaringType.IsConstructedGenericType &&
method.DeclaringType.GetIsConstructedGenericType() &&
(method.DeclaringType.GetGenericTypeDefinition() == typeof(Expression<>)) &&
(method.Name == nameof(Expression<Func<object>>.Compile));
}
Expand Down Expand Up @@ -104,7 +108,7 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
}

if ((baseResult.Method.DeclaringType != null) &&
(baseResult.Method.DeclaringType.GetTypeInfo().BaseType == typeof(MulticastDelegate)) &&
(baseResult.Method.DeclaringType.GetBaseType() == typeof(MulticastDelegate)) &&
(baseResult.Method.Name == nameof(Action.Invoke)) &&
(baseResult.Object != null) &&
(baseResult.Object.NodeType == ExpressionType.Call))
Expand Down Expand Up @@ -173,7 +177,7 @@ protected override Expression VisitUnary(UnaryExpression node)

private bool TrySubstituteExpression(
Expression expressionExpression,
IReadOnlyList<Expression> arguments,
IList<Expression> arguments,
out Expression result)
{
if (expressionExpression == null)
Expand Down
1 change: 1 addition & 0 deletions src/IharBury.Expressions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace IharBury.Expressions
{
Expand Down
4 changes: 2 additions & 2 deletions src/IharBury.Expressions/ReflectionExpressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ public static PropertyInfo TryGetPropertyInfo(LambdaExpression propertyExpressio
return methodCallExpression
.Method
.DeclaringType
.GetTypeInfo()
.DeclaredProperties.SingleOrDefault(property => property.GetMethod == methodCallExpression.Method);
.GetDeclaredProperties()
.SingleOrDefault(property => property.GetGetMethod() == methodCallExpression.Method);
return null;

default:
Expand Down
3 changes: 2 additions & 1 deletion src/IharBury.Expressions/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"System.Linq.Expressions": "4.0.10-*"
}
},
"net45": { }
"net45": { },
"net40": { }
}
}
49 changes: 49 additions & 0 deletions test/IharBury.Expressions.Tests/CompatibilityExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Reflection;

namespace IharBury.Expressions.Tests
{
internal static class CompatibilityExtensions
{
internal static Type GetBaseType(this Type type)
{
if (type == null)
throw new ArgumentNullException(nameof(type));

#if NET40
return type.BaseType;
#else
return type.GetTypeInfo().BaseType;
#endif
}

internal static IEnumerable<PropertyInfo> GetDeclaredProperties(this Type type)
{
if (type == null)
throw new ArgumentNullException(nameof(type));

#if NET40
return (IEnumerable<PropertyInfo>)type.GetProperties(BindingFlags.DeclaredOnly |
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.Public |
BindingFlags.NonPublic);
#else
return type.GetTypeInfo().DeclaredProperties;
#endif
}

internal static bool GetIsConstructedGenericType(this Type type)
{
if (type == null)
throw new ArgumentNullException(nameof(type));

#if NET40
return type.IsGenericType && !type.IsGenericTypeDefinition;
#else
return type.IsConstructedGenericType;
#endif
}
}
}
4 changes: 2 additions & 2 deletions test/IharBury.Expressions.Tests/ExpandExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ protected override Expression VisitInvocation(InvocationExpression node)
var methodCallExpression = (MethodCallExpression)node.Expression;
Assert.False(
(methodCallExpression.Method.DeclaringType != null) &&
methodCallExpression.Method.DeclaringType.IsConstructedGenericType &&
methodCallExpression.Method.DeclaringType.GetIsConstructedGenericType() &&
(methodCallExpression.Method.DeclaringType.GetGenericTypeDefinition() == typeof(Expression<>)) &&
(methodCallExpression.Method.Name == CompileMethodName),
$"The expression body has evaluation: \"{node}\".");
Expand Down Expand Up @@ -506,7 +506,7 @@ private static void ValidateMethod(Expression node, MethodInfo method)
$"The expression body has evaluation: \"{node}\".");
Assert.False(
(method.DeclaringType != null) &&
(method.DeclaringType.GetTypeInfo().BaseType == typeof(MulticastDelegate)) &&
(method.DeclaringType.GetBaseType() == typeof(MulticastDelegate)) &&
(method.Name == ReflectionExpressions.GetMethodName<Action>(action => action.Invoke())),
$"The expression body has invokation: \"{node}\".");
}
Expand Down
6 changes: 6 additions & 0 deletions test/IharBury.Expressions.Tests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
"xunit": "1.9.2",
"xunit.runner.console": "2.0.0"
}
},
"net40": {
"dependencies": {
"xunit": "1.9.2",
"xunit.runner.console": "2.0.0"
}
}
}
}

0 comments on commit 03416fd

Please sign in to comment.