forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionaryDynamicObject.cs
More file actions
31 lines (27 loc) · 1017 Bytes
/
DictionaryDynamicObject.cs
File metadata and controls
31 lines (27 loc) · 1017 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
using System.Collections.Generic;
namespace ServiceStack.Mvc
{
public class DictionaryDynamicObject : System.Dynamic.DynamicObject
{
Dictionary<string, object> Object { get; }
public DictionaryDynamicObject(Dictionary<string, object> obj) => Object = obj;
public override bool TryGetMember(System.Dynamic.GetMemberBinder binder, out object result)
{
if (!Object.TryGetValue(binder.Name, out var ret))
throw new InvalidOperationException(binder.Name);
var modelType = ret?.GetType();
if (modelType != null && (!modelType.IsPublic
&& modelType.BaseType == typeof(object)
&& modelType.DeclaringType == null))
{
result = new DictionaryDynamicObject(ret.ToObjectDictionary());
}
else
{
result = ret;
}
return true;
}
}
}