This repository was archived by the owner on Feb 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathMetadataRegistrationBase.cs
More file actions
253 lines (232 loc) · 9.98 KB
/
MetadataRegistrationBase.cs
File metadata and controls
253 lines (232 loc) · 9.98 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.Windows.Design.Metadata;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
namespace Microsoft.Toolkit.Uwp.Design.Common
{
public class MetadataRegistrationBase
{
private AttributeTable masterMetadataTable;
internal MetadataRegistrationBase() { }
/// <summary>
/// Build design time metadata attribute table.
/// </summary>
/// <returns>Custom attribute table.</returns>
protected virtual AttributeTable BuildAttributeTable()
{
AttributeTableBuilder builder = new AttributeTableBuilder();
AddDescriptions(builder);
AddAttributes(builder);
AddTables(builder, this);
masterMetadataTable = builder.CreateTable();
return masterMetadataTable;
}
/// <summary>
/// Find all AttributeTableBuilder subclasses in the assembly
/// and add their attributes to the assembly attribute table.
/// </summary>
/// <param name="builder">The assembly attribute table builder.</param>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Design time dll should not fail!")]
private void AddTables(AttributeTableBuilder builder, object parent)
{
Debug.Assert(builder != null, "AddTables is called with null parameter!");
Assembly asm = parent.GetType().Assembly;
foreach (Type t in asm.GetTypes())
{
if (t.IsSubclassOf(typeof(AttributeTableBuilder)))
{
try
{
AttributeTableBuilder atb = (AttributeTableBuilder)Activator.CreateInstance(t);
builder.AddTable(atb.CreateTable());
}
catch (Exception)
{
//error loading design assembly
}
}
}
}
/// <summary>
/// Gets or sets the case sensitive resource name of the embedded XML file.
/// </summary>
protected string XmlResourceName { get; set; }
/// <summary>
/// Gets or sets the FullName of the corresponding run time assembly.
/// </summary>
protected string AssemblyFullName { get; set; }
/// <summary>
/// Create description attribute from run time assembly xml file.
/// </summary>
/// <param name="builder">The assembly attribute table builder.</param>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Design time dll should not fail.")]
private void AddDescriptions(AttributeTableBuilder builder)
{
Debug.Assert(builder != null, "AddDescriptions is called with null parameter!");
if (string.IsNullOrEmpty(XmlResourceName) ||
string.IsNullOrEmpty(AssemblyFullName))
{
return;
}
XDocument xdoc = null;
try
{
xdoc = XDocument.Load(new StreamReader(
Assembly.GetExecutingAssembly().GetManifestResourceStream(XmlResourceName)));
}
catch { return; }
if (xdoc == null)
{
return;
}
foreach (XElement member in xdoc.Descendants("member"))
{
try
{
string name = (string)member.Attribute("name");
if (name == null)
continue;
bool isType = name.StartsWith("T:", StringComparison.OrdinalIgnoreCase);
if (isType ||
name.StartsWith("P:", StringComparison.OrdinalIgnoreCase))
{
int lastDot = name.Length;
string typeName;
if (isType)
{
typeName = name.Substring(2); // skip leading "T:"
}
else
{
lastDot = name.LastIndexOf('.');
typeName = name.Substring(2, lastDot - 2);
}
typeName += AssemblyFullName;
Type t = Type.GetType(typeName);
if (t != null && t.IsPublic && t.IsClass &&
t.IsSubclassOf(Types.PlatformTypes.DependencyObjectType))
{
string desc = ParseDescription(member);
if (desc == null)
continue;
desc = desc.Trim();
desc = string.Join(" ", desc.Split(new char[] { ' ', '\t', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries));
if (isType)
{
bool isBrowsable = true;
try
{
isBrowsable = IsBrowsable(t);
}
catch { isBrowsable = false; }
if (isBrowsable)
builder.AddCallback(t, b => b.AddCustomAttributes(new DescriptionAttribute(desc)));
else //Hide from intellisense
{
builder.AddCallback(t, b => b.AddCustomAttributes(
new BrowsableAttribute(false),
new Windows.Design.ToolboxBrowsableAttribute(false),
new ToolboxItemAttribute(false)));
}
}
else
{
string propName = name.Substring(lastDot + 1);
PropertyInfo pi = t.GetProperty(propName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
if (pi != null)
{
bool isBrowsable = true;
try
{
isBrowsable = IsBrowsable(pi);
}
catch { isBrowsable = false; }
if (isBrowsable)
builder.AddCallback(t, b => b.AddCustomAttributes(propName, new DescriptionAttribute(desc)));
else //Hide from intellisense
builder.AddCallback(t, b => b.AddCustomAttributes(new BrowsableAttribute(false)));
}
}
}
}
}
catch (Exception)
{
}
}
}
private static bool IsBrowsable(Type t)
{
var attrs = t.GetCustomAttributes(Types.PlatformTypes.EditorBrowsableAttributeType, false);
foreach (var attr in attrs)
{
return Types.PlatformTypes.IsBrowsable(attr);
}
return true;
}
private static bool IsBrowsable(PropertyInfo pi)
{
var attrs = pi.GetCustomAttributes(Types.PlatformTypes.EditorBrowsableAttributeType, false);
foreach (var attr in attrs)
{
return Types.PlatformTypes.IsBrowsable(attr);
}
return true;
}
/// <summary>
/// Create description string from xml doc summary tag.
/// </summary>
/// <param name="member">A single node of the xml doc.</param>
/// <returns>Description string.</returns>
private static string ParseDescription(XElement member)
{
string desc = null;
XElement memberDesc = member.Descendants("summary").FirstOrDefault();
if (memberDesc != null)
{
IEnumerable<XNode> nodes = memberDesc.DescendantNodes();
if (nodes != null)
{
foreach (XNode node in nodes)
{
if (node.NodeType == System.Xml.XmlNodeType.Text)
{
desc += node.ToString();
}
else
{
string s = node.ToString();
int i = s.LastIndexOf('.');
int j = s.LastIndexOf('"');
if ((i != -1 || j != -1) && j - i - 1 > 0)
{
try
{
desc += s.Substring(i + 1, j - i - 1);
}
catch { }
}
}
}
}
}
return desc;
}
/// <summary>
/// Provide a place to add custom attributes without creating a AttributeTableBuilder subclass.
/// </summary>
/// <param name="builder">The assembly attribute table builder.</param>
protected virtual void AddAttributes(AttributeTableBuilder builder)
{
}
}
}