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
103 lines (93 loc) · 3.62 KB
/
Copy pathExamples.cs
File metadata and controls
103 lines (93 loc) · 3.62 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
// --------------------------------------------------------------------------------------------------------------------
// <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
{
#if !UNIVERSAL
public static IEnumerable<T> GetCustomAttributes<T>(this Type type) where T : Attribute
{
return type.GetCustomAttributes(typeof(T), true).Cast<T>();
}
public static IEnumerable<T> GetCustomAttributes<T>(this MethodInfo info) where T : Attribute
{
return info.GetCustomAttributes(typeof(T), true).Cast<T>();
}
#endif
/// <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.GetCustomAttributes<ExamplesAttribute>().FirstOrDefault();
if (examplesAttribute == null)
{
continue;
}
var examplesTags = type.GetCustomAttributes<TagsAttribute>().FirstOrDefault() ?? new TagsAttribute();
var types = new List<Type>();
var baseType = type;
while (baseType != null)
{
#if UNIVERSAL
types.Add(baseType.AsType());
baseType = baseType.BaseType != null ? baseType.BaseType.GetTypeInfo() : 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.GetCustomAttributes<ExampleAttribute>().FirstOrDefault();
if (exampleAttribute != null)
{
var exampleTags = method.GetCustomAttributes<TagsAttribute>().FirstOrDefault() ?? 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;
}
}
}