forked from oxyplot/oxyplot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExamples.cs
More file actions
117 lines (107 loc) · 4.15 KB
/
Examples.cs
File metadata and controls
117 lines (107 loc) · 4.15 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
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Examples.cs" company="OxyPlot">
// Copyright (c) 2014 OxyPlot contributors
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace ExampleLibrary
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
/// <summary>
/// Enumerates all examples in the assembly.
/// </summary>
public static class Examples
{
/// <summary>
/// Gets the first or default attribute of the specified type.
/// </summary>
/// <typeparam name="T">The attribute type.</typeparam>
/// <param name="type">The type to reflect.</param>
/// <returns>The attribute.</returns>
public static T FirstOrDefault<T>(this Type type) where T : Attribute
{
var attributes = type.GetCustomAttributes(typeof(T), true).ToArray();
return attributes.Length == 0 ? null : (T)attributes[0];
}
/// <summary>
/// Gets the first or default attribute of the specified type.
/// </summary>
/// <typeparam name="T">The attribute type.</typeparam>
/// <param name="info">The information.</param>
/// <returns>
/// The attribute.
/// </returns>
public static T FirstOrDefault<T>(this MethodInfo info) where T : Attribute
{
var attributes = info.GetCustomAttributes(typeof(T), true).ToArray();
return attributes.Length == 0 ? null : (T)attributes[0];
}
/// <summary>
/// Gets the list of examples.
/// </summary>
/// <returns>The list of examples.</returns>
public static List<ExampleInfo> GetList()
{
var list = new List<ExampleInfo>();
#if UNIVERSAL
var assemblyTypes = typeof(Examples).GetTypeInfo().Assembly.DefinedTypes;
#else
var assemblyTypes = typeof(Examples).Assembly.GetTypes();
#endif
foreach (var type in assemblyTypes)
{
var examplesAttribute = type.FirstOrDefault<ExamplesAttribute>();
if (examplesAttribute == null)
{
continue;
}
var examplesTags = type.FirstOrDefault<TagsAttribute>() ?? new TagsAttribute();
var types = new List<Type>();
var baseType = type;
while (baseType != null)
{
#if UNIVERSAL
types.Add(baseType.AsType());
baseType = null;
#else
types.Add(baseType);
baseType = baseType.BaseType;
#endif
}
foreach (var t in types)
{
#if UNIVERSAL
var methods = t.GetRuntimeMethods();
#else
var methods = t.GetMethods(BindingFlags.Public | BindingFlags.Static);
#endif
foreach (var method in methods)
{
try
{
var exampleAttribute = method.FirstOrDefault<ExampleAttribute>();
if (exampleAttribute != null)
{
var exampleTags = method.FirstOrDefault<TagsAttribute>() ?? new TagsAttribute();
var tags = new List<string>(examplesTags.Tags);
tags.AddRange(exampleTags.Tags);
list.Add(
new ExampleInfo(
examplesAttribute.Category,
exampleAttribute.Title,
tags.ToArray(),
method));
}
}
catch (Exception)
{
}
}
}
}
return list;
}
}
}