Files
Umbraco-CMS/umbraco.MacroEngines.Juno/DynamicDictionary.cs
agrath@gmail.com bcf735f4f8 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.
2011-02-06 19:52:05 -13:00

42 lines
1.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;
namespace umbraco.MacroEngines
{
public class DynamicDictionary : DynamicObject
{
Dictionary<string, object> _dictionary;
public DynamicDictionary(Dictionary<string, object> sourceItems)
{
_dictionary = sourceItems;
}
public override bool TrySetMember(SetMemberBinder binder, object 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)
{
if (_dictionary != null)
{
if (_dictionary.TryGetValue(binder.Name.ToLower(), out result))
{
return true;
}
}
result = null;
return true;
}
}
}