forked from zhongkaifu/TensorSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionExtensions.cs
More file actions
61 lines (58 loc) · 2.16 KB
/
Copy pathReflectionExtensions.cs
File metadata and controls
61 lines (58 loc) · 2.16 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
// Copyright (c) Zhongkai Fu. All rights reserved.
// https://github.com/zhongkaifu/TensorSharp
//
// This file is part of TensorSharp.
//
// TensorSharp is licensed under the BSD-3-Clause license found in the LICENSE file in the root directory of this source tree.
//
// TensorSharp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the BSD-3-Clause License for more details.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace TensorSharp
{
public static class AssemblyExtensions
{
public static IEnumerable<Tuple<Type, IEnumerable<T>>> TypesWithAttribute<T>(this Assembly assembly, bool inherit)
{
foreach (Type type in assembly.GetTypes())
{
object[] attributes = type.GetCustomAttributes(typeof(T), inherit);
if (attributes.Any())
{
yield return Tuple.Create(type, attributes.Cast<T>());
}
}
}
}
public static class TypeExtensions
{
public static IEnumerable<Tuple<MethodInfo, IEnumerable<T>>> MethodsWithAttribute<T>(this Type type, bool inherit)
{
foreach (MethodInfo method in type.GetMethods())
{
object[] attributes = method.GetCustomAttributes(typeof(T), inherit);
if (attributes.Any())
{
yield return Tuple.Create(method, attributes.Cast<T>());
}
}
}
}
public static class MethodExtensions
{
public static IEnumerable<Tuple<ParameterInfo, IEnumerable<T>>> ParametersWithAttribute<T>(this MethodInfo method, bool inherit)
{
foreach (ParameterInfo paramter in method.GetParameters())
{
object[] attributes = paramter.GetCustomAttributes(typeof(T), inherit);
if (attributes.Any())
{
yield return Tuple.Create(paramter, attributes.Cast<T>());
}
}
}
}
}