-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #931 from riganti/feature/lambdas-extension-methods
Add support for using extension methods in bindings
- Loading branch information
Showing
22 changed files
with
656 additions
and
434 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/DotVVM.Framework.Tests.Common/Binding/CustomExtensionMethodTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq.Expressions; | ||
using System.Text; | ||
using DotVVM.Framework.Compilation.Binding; | ||
using DotVVM.Framework.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace DotVVM.Framework.Tests.Common.Binding | ||
{ | ||
[TestClass] | ||
public class CustomExtensionMethodTests | ||
{ | ||
private DotvvmConfiguration configuration; | ||
private MemberExpressionFactory memberExpressionFactory; | ||
|
||
[TestInitialize] | ||
public void Init() | ||
{ | ||
this.configuration = DotvvmTestHelper.CreateConfiguration(services => services.AddScoped<IExtensionsProvider, TestExtensionsProvider>()); | ||
this.memberExpressionFactory = configuration.ServiceProvider.GetRequiredService<MemberExpressionFactory>(); | ||
} | ||
|
||
[TestMethod] | ||
public void Call_FindCustomExtensionMethod() | ||
{ | ||
var target = new MethodGroupExpression() | ||
{ | ||
MethodName = nameof(TestExtensions.Increment), | ||
Target = Expression.Constant(11) | ||
}; | ||
|
||
var expression = memberExpressionFactory.Call(target, Array.Empty<Expression>()); | ||
var result = Expression.Lambda<Func<int>>(expression).Compile().Invoke(); | ||
Assert.AreEqual(12, result); | ||
} | ||
} | ||
|
||
static class TestExtensions | ||
{ | ||
public static int Increment(this int number) | ||
=> ++number; | ||
} | ||
|
||
class TestExtensionsProvider : DefaultExtensionsProvider | ||
{ | ||
public TestExtensionsProvider() | ||
{ | ||
AddTypeForExtensionsLookup(typeof(TestExtensions)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/DotVVM.Framework/Compilation/Binding/DefaultExtensionsProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace DotVVM.Framework.Compilation.Binding | ||
{ | ||
public class DefaultExtensionsProvider : IExtensionsProvider | ||
{ | ||
private readonly List<Type> typesLookup; | ||
private readonly List<MethodInfo> methodsLookup; | ||
|
||
public DefaultExtensionsProvider() | ||
{ | ||
typesLookup = new List<Type>(); | ||
methodsLookup = new List<MethodInfo>(); | ||
AddTypeForExtensionsLookup(typeof(Enumerable)); | ||
} | ||
|
||
protected void AddTypeForExtensionsLookup(Type type) | ||
{ | ||
foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Static).Where(m => m.GetCustomAttribute(typeof(ExtensionAttribute)) != null)) | ||
methodsLookup.Add(method); | ||
|
||
typesLookup.Add(type); | ||
} | ||
|
||
public virtual IEnumerable<MethodInfo> GetExtensionMethods() | ||
{ | ||
return methodsLookup; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.