-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathAttachedEntitiesHelper.cs
More file actions
279 lines (233 loc) · 13.4 KB
/
AttachedEntitiesHelper.cs
File metadata and controls
279 lines (233 loc) · 13.4 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;
using Mono.Collections.Generic;
using Mono.Documentation.Updater;
namespace Mono.Documentation.Util
{
public static class AttachedEntitiesHelper
{
private const string PropertyConst = "Property";
private const string EventConst = "Event";
private static readonly int EventLength = EventConst.Length;
private static readonly int PropertyLength = PropertyConst.Length;
public static string GetEventName(string fieldDefinitionName)
{
return fieldDefinitionName.Substring(0, fieldDefinitionName.Length - EventLength);
}
public static string GetPropertyName(string fieldDefinitionName)
{
return fieldDefinitionName.Substring(0, fieldDefinitionName.Length - PropertyLength);
}
public static IEnumerable<MemberReference> GetAttachedEntities(TypeDefinition type)
{
var methodsLookUpTable = GetMethodsLookUpTable(type);
foreach (var attachedEventReference in GetAttachedEvents(type, methodsLookUpTable))
{
yield return attachedEventReference;
}
foreach (var attachedPropertyReference in GetAttachedProperties(type, methodsLookUpTable))
{
yield return attachedPropertyReference;
}
}
private static Dictionary<string, IEnumerable<MethodDefinition>> GetMethodsLookUpTable(TypeDefinition type)
{
return type.Methods.GroupBy(i => i.Name, i => i).ToDictionary(i => i.Key, i => i.AsEnumerable());
}
#region Attached Events
private static IEnumerable<AttachedEventReference> GetAttachedEvents(TypeDefinition type, Dictionary<string, IEnumerable<MethodDefinition>> methods)
{
foreach (var field in type.Fields)
{
if (IsAttachedEvent(field, methods))
yield return new AttachedEventReference(field);
}
}
private static bool IsAttachedEvent(FieldDefinition field, Dictionary<string, IEnumerable<MethodDefinition>> methods)
{
// https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/attached-events-overview
if (!field.Name.EndsWith(EventConst))
return false;
var addMethodName = $"Add{GetEventName(field.Name)}Handler";
var removeMethodName = $"Remove{GetEventName(field.Name)}Handler";
return
// WPF implements attached events as routed events; the identifier to use for an event (RoutedEvent) is already defined by the WPF event system
(IsAssignableTo(field.FieldType, Consts.RoutedEventFullName) ||
IsAssignableTo(field.FieldType, Consts.RoutedEventFullNameWinRT) ||
IsAssignableTo(field.FieldType, Consts.RoutedEventFullNameWinUI))
&& field.IsPublic
&& field.IsStatic
&& field.IsInitOnly
// Has a method Add*Handler with two parameters.
// Has a method Remove*Handler with two parameters.
&& methods.ContainsKey(addMethodName)
&& methods.ContainsKey(removeMethodName)
&& methods[addMethodName].Any(IsAttachedEventMethod)
&& methods[removeMethodName].Any(IsAttachedEventMethod);
}
private static bool IsAttachedEventMethod(MethodDefinition method)
{
// The method must be public and static, with no return value.
return method.IsPublic
&& method.IsStatic
&& method.ReturnType.FullName == Consts.VoidFullName
&& AreAttachedEventMethodParameters(method.Parameters);
}
private static bool AreAttachedEventMethodParameters(Collection<ParameterDefinition> parameters)
{
if (parameters.Count != 2)
return false;
return
// The first parameter is DependencyObject
(IsAssignableTo(parameters[0].ParameterType, Consts.DependencyObjectFullName) ||
IsAssignableTo(parameters[0].ParameterType, Consts.DependencyObjectFullNameWinRT) ||
IsAssignableTo(parameters[0].ParameterType, Consts.DependencyObjectFullNameWinUI))
// The second parameter is the handler to add/remove
&& IsAttachedEventHandler(parameters[1].ParameterType);
}
private static bool IsAttachedEventHandler(TypeReference typeReference)
{
var type = typeReference.Resolve();
if (!DocUtils.IsDelegate(type))
return false;
MethodDefinition invoke = type.GetMethod("Invoke");
return invoke.Parameters.Count == 2;
}
#endregion
#region Attached Properties
private static IEnumerable<AttachedPropertyReference> GetAttachedProperties(TypeDefinition type, Dictionary<string, IEnumerable<MethodDefinition>> methods)
{
foreach (var field in type.Fields)
{
if (IsAttachedProperty(field, methods))
yield return new AttachedPropertyReference(field);
}
foreach (var property in type.Properties.Where(t => t.PropertyType.FullName == Consts.DependencyPropertyFullName
|| t.PropertyType.FullName == Consts.DependencyPropertyFullNameWindowsXaml
|| t.PropertyType.FullName == Consts.DependencyPropertyFullNameMicrosoftXaml))
{
if (IsAttachedProperty(property, methods))
yield return new AttachedPropertyReference(property);
}
}
private static bool IsAttachedProperty(FieldDefinition field, Dictionary<string, IEnumerable<MethodDefinition>> methods)
{
// https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/attached-properties-overview
// https://github.com/mono/api-doc-tools/issues/63#issuecomment-328995418
if (!field.Name.EndsWith(PropertyConst, StringComparison.Ordinal))
return false;
var propertyName = GetPropertyName(field.Name);
var getMethodName = $"Get{propertyName}";
var setMethodName = $"Set{propertyName}";
var hasDualPropertyConst = propertyName.EndsWith(PropertyConst, StringComparison.Ordinal);
var hasExistingProperty = field?.DeclaringType?.Properties.Any (p => p.Name.Equals (propertyName, StringComparison.Ordinal) && GetCheckVisible(p.Resolve()));
var hasExistingField = hasDualPropertyConst ? false :
field?.DeclaringType?.Fields.Any (f => f.Name.Equals (propertyName, StringComparison.Ordinal) && GetCheckVisible(f.Resolve()));
return !hasExistingProperty.IsTrue () && !hasExistingField.IsTrue () &&
// Class X has a static field of type DependencyProperty [Name]Property
(field.FieldType.FullName == Consts.DependencyPropertyFullName || field.FieldType.FullName == Consts.DependencyPropertyFullNameWindowsXaml
|| field.FieldType.FullName == Consts.DependencyPropertyFullNameMicrosoftXaml)
&& field.IsPublic
&& field.IsStatic
&& field.IsInitOnly
// Class X also has static methods with the following names: Get[Name] or Set[Name]
&& ((methods.ContainsKey(getMethodName) && methods[getMethodName].Any(IsAttachedPropertyGetMethod))
|| (methods.ContainsKey(setMethodName) && methods[setMethodName].Any(IsAttachedPropertySetMethod)));
}
private static bool IsAttachedProperty(PropertyDefinition property, Dictionary<string, IEnumerable<MethodDefinition>> methods)
{
if (!property.Name.EndsWith(PropertyConst, StringComparison.Ordinal))
return false;
var propertyName = GetPropertyName(property.Name);
var getMethodName = $"Get{propertyName}";
var setMethodName = $"Set{propertyName}";
var hasExistingProperty = property?.DeclaringType?.Properties.Any(p => p.Name.Equals(propertyName, StringComparison.Ordinal) && GetCheckVisible(p.Resolve()));
var hasExistingField = property?.DeclaringType?.Fields.Any(f => f.Name.Equals(propertyName, StringComparison.Ordinal) && GetCheckVisible(f.Resolve()));
return !hasExistingProperty.IsTrue() && !hasExistingField.IsTrue() &&
// Class X has a static field of type DependencyProperty [Name]Property
(property.PropertyType.FullName == Consts.DependencyPropertyFullName || property.PropertyType.FullName == Consts.DependencyPropertyFullNameWindowsXaml
|| property.PropertyType.FullName == Consts.DependencyPropertyFullNameMicrosoftXaml)
// Class X also has static methods with the following names: Get[Name] or Set[Name]
&& ((methods.ContainsKey(getMethodName) && methods[getMethodName].Any(IsAttachedPropertyGetMethod))
|| (methods.ContainsKey(setMethodName) && methods[setMethodName].Any(IsAttachedPropertySetMethod)));
}
private static bool IsAttachedPropertyGetMethod(MethodDefinition method)
{
return method.Parameters.Count == 1
// returns a value of type dp.PropertyType (or IsAssignableTo…), where dp is the value of the static field.
// && IsAssignableTo(method.ReturnType, "");
// The Get method takes one argument of type DependencyObject(or something IsAssignableTo(DependencyObject),
&& (IsAssignableTo(method.Parameters[0].ParameterType, Consts.DependencyObjectFullName) ||
IsAssignableTo(method.Parameters[0].ParameterType, Consts.DependencyObjectFullNameWinRT) ||
IsAssignableTo(method.Parameters[0].ParameterType, Consts.DependencyObjectFullNameWinUI));
}
private static bool IsAttachedPropertySetMethod(MethodDefinition method)
{
return method.Parameters.Count == 2// The Set method takes two arguments.
// The first has type DependencyObject(or IsAssignableTo…),
&& (IsAssignableTo(method.Parameters[0].ParameterType, Consts.DependencyObjectFullName) ||
IsAssignableTo(method.Parameters[0].ParameterType, Consts.DependencyObjectFullNameWinRT) ||
IsAssignableTo(method.Parameters[0].ParameterType, Consts.DependencyObjectFullNameWinUI) ||
IsAssignableTo(method.Parameters[0].ParameterType, Consts.DependencyPropertyFullNameIInputElement) ||
IsAssignableTo(method.Parameters[0].ParameterType, Consts.DependencyPropertyFullNameObject))
// the second has type dp.PropertyType (or IsAssignableTo…).
// && IsAssignableTo(method.Parameters[1].ParameterType, "")
// It returns void.
&& method.ReturnType.FullName == Consts.VoidFullName;
}
#endregion
private static bool IsAssignableTo(TypeReference type, string targetTypeName)
{
if (type == null)
return false;
var typeDefenition = type.Resolve();
if (typeDefenition == null || typeDefenition.IsSealed)
return type.FullName == targetTypeName;
return type.FullName == targetTypeName || IsAssignableTo(typeDefenition.BaseType, targetTypeName);
}
private static bool GetCheckVisible(IMemberDefinition member)
{
if (member == null)
throw new ArgumentNullException("member");
PropertyDefinition prop = member as PropertyDefinition;
if (prop != null)
return ChkPropertyVisible(prop);
FieldDefinition field = member as FieldDefinition;
if (field != null)
return ChkFieldVisible(field);
return false;
}
private static bool ChkPropertyVisible(PropertyDefinition property)
{
MethodDefinition method;
bool get_visible = false;
bool set_visible = false;
if ((method = property.GetMethod) != null &&
(DocUtils.IsExplicitlyImplemented(method) ||
(!method.IsPrivate && !method.IsAssembly && !method.IsFamilyAndAssembly)))
get_visible = true;
if ((method = property.SetMethod) != null &&
(DocUtils.IsExplicitlyImplemented(method) ||
(!method.IsPrivate && !method.IsAssembly && !method.IsFamilyAndAssembly)))
set_visible = true;
if ((set_visible == false) && (get_visible == false))
return false;
else
return true;
}
private static bool ChkFieldVisible(FieldDefinition field)
{
TypeDefinition declType = (TypeDefinition)field.DeclaringType;
if (declType.IsEnum && field.Name == "value__")
return false; // This member of enums aren't documented.
return field.IsPublic || field.IsFamily || field.IsFamilyOrAssembly;
}
}
internal static class NBoolExtensions
{
public static bool IsTrue (this Nullable<bool> value) =>
value.HasValue && value.Value;
}
}