-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathTypeEx.cs
More file actions
48 lines (45 loc) · 1.54 KB
/
TypeEx.cs
File metadata and controls
48 lines (45 loc) · 1.54 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
using System;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
namespace UnityHeapEx
{
public static class TypeEx
{
/// <summary>
/// Enumerates all fields of a type, including those defined in base types.
/// </summary>
public static IEnumerable<FieldInfo> EnumerateAllFields(this Type type)
{
var @base = type.BaseType;
if (@base != null)
foreach (var field in EnumerateAllFields(@base))
{
yield return field;
}
foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
yield return field;
}
}
/// <summary>
/// Formats type name, including generic type arguments if any. Also changes angle brackets
/// to parenteses so that type is more readable in xml
/// </summary>
public static string GetFormattedName(this Type type)
{
var name = type.Name;
name = name.Replace( "<", "(" ).Replace( ">", ")" ); // makes XML easier to read
if (type.IsGenericType && !type.ContainsGenericParameters)
{
name += "(" + String.Join(", ", type.GetGenericArguments().Select<Type, String>(GetFormattedName).ToArray()) +
")";
}
if (type.IsNested)
{
name = GetFormattedName(type.DeclaringType) + "." + name;
}
return name;
}
}
}