-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeModelFactory.cs
More file actions
52 lines (48 loc) · 1.62 KB
/
Copy pathTypeModelFactory.cs
File metadata and controls
52 lines (48 loc) · 1.62 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace DataKit.Modelling.TypeModels
{
internal static class TypeModelFactory
{
/// <summary>
/// Cached reference to the generic build model method.
/// </summary>
private static readonly MethodInfo _createModelMethod = typeof(TypeModelFactory)
.GetMethod(nameof(BuildTypeModelGeneric), BindingFlags.Static | BindingFlags.NonPublic);
/// <summary>
/// Build the TypeModel for the provided Type.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static TypeModel BuildTypeModel(Type type)
=> _createModelMethod.MakeGenericMethod(type).Invoke(null, null) as TypeModel;
/// <summary>
/// Build a TypeModel for the provided generic type parameter.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
private static TypeModel<T> BuildTypeModelGeneric<T>()
{
var fields = GetPropertyFields(typeof(T));
return new TypeModel<T>(fields);
}
/// <summary>
/// Gets all eligable fields for the type model of the provided Type.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
private static IReadOnlyList<PropertyField> GetPropertyFields(Type type)
{
var ret = new List<PropertyField>();
foreach (var property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(q => (q.CanRead && !q.GetMethod.IsStatic) ||
(q.CanWrite && !q.SetMethod.IsStatic)))
{
ret.Add(PropertyField.CreateFromPropertyInfo(property));
}
return ret;
}
}
}