Skip to content

Commit

Permalink
Initial support for DynamicObject members
Browse files Browse the repository at this point in the history
Since DynamicObject objects require a compiler, referenced
Microsoft.CSharp to do the compiling. For the time being, using
Dynamitey as a dependency since it provides the
Dynamic.InvokeGet(object, memberName) function, which otherwise would
require a large amount of additional work, which the Dynamitey.Dynamic
static class already provides. Perhaps the parts of Dynamitey that were
used could be imported into the Mustache# project in a later change.
This has been tested using DynamicObject objects that have members, but
I'm not sure yet how to interface this with any type of indexed
DynamicObjects, for instance, a dynamic IList<T>. It isn't completely
clear how to find the domain of the function without stepping through
through all the valid indexes in the domain; for instance, we could
presume it starts at [0] where [0] != null and ends at [0...i-1] where
[i] == null. I still need to interrogate the library further to find out
where Scope/Generator figures out that it has a list on its hand instead
of an object; or, whether this only occurs with the employ of helpers
such as {{#each}}{{/each}}. In any event, that might be where to
interrogate the Dynamic.InvokeGetIndex(instance, { index }). For now,
I've tested this to work with a specialized System.Dynamic.DynamicObject
class, which does override the TryGetMember() method, and Mustache# does
in fact invoke this method, thanks to Dynamitey.
  • Loading branch information
daball committed Nov 29, 2014
1 parent 76a3f60 commit 05a7da4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
40 changes: 40 additions & 0 deletions mustache-sharp/PropertyDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.Linq;
using System.Reflection;

Expand Down Expand Up @@ -58,12 +59,51 @@ public PropertyDictionary(object instance)
{
typeCache.Add(fieldInfo.Name, i => fieldInfo.GetValue(i));
}

var dynamicMembers = getDynamicMembers(instance);
foreach (KeyValuePair<string, object> member in dynamicMembers)
{
typeCache.Add(member.Key, i => member.Value);
}

_cache.Add(type, typeCache);
}
return typeCache;
}

private static IEnumerable<KeyValuePair<string, object>> getDynamicMembers(object instance)
{
Dictionary<string, object> members = new Dictionary<string, object>();

Type type = instance.GetType();
Type baseType = type.BaseType;
bool isDynamicObject = false;

if (baseType == null)
return members;

while (baseType != baseType.BaseType && baseType.BaseType != null)
{
isDynamicObject = (baseType.FullName == "System.Dynamic.DynamicObject");
if (isDynamicObject)
break;
else
baseType = baseType.BaseType;
}

if (isDynamicObject)
{
DynamicObject dynamicInstance = (DynamicObject)instance;
dynamic dynamicObject = dynamicInstance;

foreach (string member in dynamicInstance.GetDynamicMemberNames())
{
members.Add(member, Dynamitey.Dynamic.InvokeGet(instance, member));
}
}
return members;
}

private static IEnumerable<TMember> getMembers<TMember>(Type type, IEnumerable<TMember> members)
where TMember : MemberInfo
{
Expand Down
7 changes: 7 additions & 0 deletions mustache-sharp/mustache-sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Dynamitey">
<HintPath>..\..\packages\Dynamitey.1.0.2.0\lib\net40\Dynamitey.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -86,6 +90,9 @@
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
4 changes: 4 additions & 0 deletions mustache-sharp/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Dynamitey" version="1.0.2.0" targetFramework="net40" />
</packages>

0 comments on commit 05a7da4

Please sign in to comment.