Added support for DynamicLinq in place of Lambda syntax. The short version is you can't use a Lambda with a DynamicNode
e.g. @Model.Children.Where(node=>node.shouldBeVisible)
To solve this, I used the DynamicQueryable class from the Linq samples which has a parser that can take a string then modified the internals a bit so
that if your object is a DynamicObject, an additional expression tree is generated which calls the TryGetMember on it
The end result is that you can now do this [I have Random(this DynamicNodeList nodes, int max) in my bin folder]
@Model.Children.Where("shouldBeVisible").Random(2) => two nodes, randomly picked, from the ones that should be visible
*Only* Where is implemented here currently, I'll add support by OrderBy and ThenBy after I've tested some more complex scenarios.
I need to fix a small issue with my DynamicLoading of extensions - under some scenarios the class doesn't get found and i'm not sure why.
This commit is contained in:
@@ -15,7 +15,14 @@ namespace umbraco.MacroEngines
|
||||
}
|
||||
public override bool TrySetMember(SetMemberBinder binder, object value)
|
||||
{
|
||||
_dictionary[binder.Name] = value;
|
||||
if (_dictionary.ContainsKey(binder.Name))
|
||||
{
|
||||
_dictionary[binder.Name.ToLower()] = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
_dictionary.Add(binder.Name.ToLower(), value);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public override bool TryGetMember(GetMemberBinder binder, out object result)
|
||||
|
||||
@@ -370,7 +370,11 @@ namespace umbraco.MacroEngines
|
||||
{
|
||||
get { if (n == null) return null; return n.Name; }
|
||||
}
|
||||
|
||||
public bool Visible
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
public string Url
|
||||
{
|
||||
get { if (n == null) return null; return n.Url; }
|
||||
|
||||
@@ -8,7 +8,8 @@ using System.Collections;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Web.Compilation;
|
||||
|
||||
using System.Linq.Expressions;
|
||||
using System.Linq.Dynamic;
|
||||
namespace umbraco.MacroEngines
|
||||
{
|
||||
public class DynamicNodeList : DynamicObject, IEnumerable
|
||||
@@ -31,6 +32,13 @@ namespace umbraco.MacroEngines
|
||||
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
|
||||
{
|
||||
var name = binder.Name;
|
||||
if (name == "Where")
|
||||
{
|
||||
string predicate = args.First().ToString();
|
||||
var values = args.Skip(1).ToArray();
|
||||
result = new DynamicNodeList(this.Where<DynamicNode>(predicate, values).ToList());
|
||||
return true;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
@@ -209,5 +217,10 @@ namespace umbraco.MacroEngines
|
||||
{
|
||||
return Items.GetEnumerator();
|
||||
}
|
||||
|
||||
public IQueryable<T> Where<T>(string predicate, params object[] values)
|
||||
{
|
||||
return ((IQueryable<T>)Items.AsQueryable()).Where(predicate, values);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,9 +63,11 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="DLRScriptingEngine.cs" />
|
||||
<Compile Include="DynamicDictionary.cs" />
|
||||
<Compile Include="DynamicQueryableBinders.cs" />
|
||||
<Compile Include="DynamicMedia.cs" />
|
||||
<Compile Include="DynamicNode.cs" />
|
||||
<Compile Include="DynamicNodeList.cs" />
|
||||
<Compile Include="DynamicQueryable.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="RazorEngine.cs" />
|
||||
<Compile Include="Scripting\MacroScript.cs" />
|
||||
|
||||
Reference in New Issue
Block a user