forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRazorDynamicObject.cs
More file actions
62 lines (56 loc) · 1.89 KB
/
RazorDynamicObject.cs
File metadata and controls
62 lines (56 loc) · 1.89 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Diagnostics;
using System.Dynamic;
namespace ServiceStack.Razor.Compilation
{
/// <summary>
/// Defines a dynamic object.
/// </summary>
internal class RazorDynamicObject : DynamicObject
{
/// <summary>
/// Gets or sets the model.
/// </summary>
public object Model { get; set; }
/// <summary>
/// Gets the value of the specified member.
/// </summary>
/// <param name="binder">The current binder.</param>
/// <param name="result">The member result.</param>
/// <returns>True.</returns>
[DebuggerStepThrough]
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
var dynamicObject = Model as RazorDynamicObject;
if (dynamicObject != null)
return dynamicObject.TryGetMember(binder, out result);
Type modelType = Model.GetType();
var prop = modelType.GetProperty(binder.Name);
if (prop == null)
{
result = null;
return false;
}
object value = prop.GetValue(Model, null);
if (value == null)
{
result = value;
return true;
}
Type valueType = value.GetType();
result = (CompilerServices.IsAnonymousType(valueType))
? new RazorDynamicObject { Model = value }
: value;
return true;
}
public override bool TryConvert(ConvertBinder binder, out object result)
{
if (binder.ReturnType == Model.GetType())
{
result = Model;
return true;
}
return base.TryConvert(binder, out result);
}
}
}