-
Notifications
You must be signed in to change notification settings - Fork 5
/
System.Reflection - converting Field, Method and PropertyInfo to yUML.linq
86 lines (71 loc) · 2.57 KB
/
System.Reflection - converting Field, Method and PropertyInfo to yUML.linq
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
<Query Kind="Program" />
void Main()
{
typeof(MyClass).GetYuml().Dump();
}
static class TypeExtenions
{
public static string GetYuml(this Type type)
{
string getAnyDetails()
{
var fields = type
.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.OfType<FieldInfo>()
.Where(i => !i.Name.EndsWith("__BackingField"));
fields.Dump($"{nameof(FieldInfo)}");
var methods = type
.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.OfType<MethodInfo>()
.Where(i => (i.DeclaringType == type) && !i.IsSpecialName);
methods.Dump($"{nameof(MethodInfo)}");
var properties = type
.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.OfType<PropertyInfo>();
properties.Dump($"{nameof(PropertyInfo)}");
var builder = new StringBuilder();
if (properties.Any()) builder.Append("|");
foreach (var info in properties)
{
if (info.GetMethod.IsFamily) builder.Append("#");
else if (info.GetMethod.IsPrivate) builder.Append("-");
else if (info.GetMethod.IsPublic) builder.Append("+");
builder.AppendFormat($"{info.Name};");
}
if (methods.Any()) builder.Append("|");
foreach (var info in methods)
{
if (info.IsFamily) builder.Append("#");
else if (info.IsPrivate) builder.Append("-");
else if (info.IsPublic) builder.Append("+");
builder.AppendFormat($"{info.Name}();");
}
if (fields.Any()) builder.Append("|");
foreach (var info in fields)
{
if (info.IsFamily) builder.Append("#");
else if (info.IsPrivate) builder.Append("-");
else if (info.IsPublic) builder.Append("+");
builder.AppendFormat($"{info.Name};");
}
return builder.ToString();
}
var yUml = $"[{type.Name}{getAnyDetails()}]";
return yUml;
}
}
public class MyClass
{
public MyClass()
{
myField = "Hello world.";
}
public int MyProperty { get; set; }
public string MyOtherProperty { get; set; }
protected string MyProtectedProperty { get; set; }
public string GetHello()
{
return myField;
}
readonly string myField;
}