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:
agrath@gmail.com
2011-02-06 19:52:05 -13:00
parent eba7c9d550
commit bcf735f4f8
4 changed files with 29 additions and 3 deletions

View File

@@ -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)

View File

@@ -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; }

View File

@@ -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);
}
}
}

View File

@@ -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" />