/
ActionDescriptor.cs
95 lines (82 loc) · 2.54 KB
/
ActionDescriptor.cs
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
namespace Microsoft.VisualStudio.Web.CodeGeneration
{
public class ActionDescriptor
{
private Type _actionModel;
private List<ParameterDescriptor> _parameters;
public ActionDescriptor(CodeGeneratorDescriptor descriptor,
MethodInfo method)
{
if (descriptor == null)
{
throw new ArgumentNullException(nameof(descriptor));
}
if (method == null)
{
throw new ArgumentNullException(nameof(method));
}
Generator = descriptor;
ActionMethod = method;
}
public MethodInfo ActionMethod
{
get;
private set;
}
public Type ActionModel
{
get
{
if (_actionModel == null)
{
var parameters = ActionMethod.GetParameters();
Debug.Assert(parameters.Count() == 1);
_actionModel = parameters.First().ParameterType;
}
return _actionModel;
}
}
public CodeGeneratorDescriptor Generator
{
get;
private set;
}
public List<ParameterDescriptor> Parameters
{
get
{
if (_parameters == null)
{
_parameters = ActionModel
.GetRuntimeProperties()
.Where(pi => IsCandidateProperty(pi))
.Select(pi => new ParameterDescriptor(pi))
.ToList();
}
return _parameters;
}
}
private bool IsCandidateProperty(PropertyInfo property)
{
if (property == null)
{
throw new ArgumentNullException(nameof(property));
}
return property.CanWrite &&
property.GetIndexParameters().Length == 0 &&
IsSupportedPropertyType(property.PropertyType);
}
private bool IsSupportedPropertyType(Type propertyType)
{
return propertyType == typeof(string) ||
propertyType == typeof(bool);
}
}
}