forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionEx.cs
More file actions
57 lines (49 loc) · 1.78 KB
/
Copy pathReflectionEx.cs
File metadata and controls
57 lines (49 loc) · 1.78 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
// -----------------------------------------------------------------------
// <copyright file="ReflectionEx.cs" company="Asynkron HB">
// Copyright (C) 2015-2017 Asynkron HB All rights reserved
// </copyright>
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
#if NET45
#endif
namespace Wire.Extensions
{
public static class BindingFlagsEx
{
public const BindingFlags All = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
}
public static class ReflectionEx
{
public static readonly Assembly CoreAssembly = typeof(int).GetTypeInfo().Assembly;
public static FieldInfo[] GetFieldInfosForType(this Type type)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
var fieldInfos = new List<FieldInfo>();
var current = type;
while (current != null)
{
var tfields =
current
.GetTypeInfo()
.GetFields(BindingFlagsEx.All)
#if NET45
.Where(f => !f.IsDefined(typeof(NonSerializedAttribute)))
#endif
.Where(f => !f.IsStatic)
.Where(f => f.FieldType != typeof(IntPtr))
.Where(f => f.FieldType != typeof(UIntPtr))
.Where(f => f.Name != "_syncRoot"); //HACK: ignore these
fieldInfos.AddRange(tfields);
current = current.GetTypeInfo().BaseType;
}
var fields = fieldInfos.OrderBy(f => f.Name).ToArray();
return fields;
}
}
}