-
Notifications
You must be signed in to change notification settings - Fork 1
/
TypeExtensions.cs
46 lines (41 loc) · 1.74 KB
/
TypeExtensions.cs
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
using System;
using System.Linq;
using System.Reflection;
namespace Immutable_Deque_visualization
{
public static class TypeExtensions
{
public static string GetShortName(this Type type)
{
string result = type.Name;
if (type.IsGenericType)
{
if (type.Name.Contains('`'))
{
// remove genric indication (e.g. `1)
result = result.Substring(0, result.LastIndexOf('`'));
}
var genericArguments = type.GetGenericArguments();
if (genericArguments.Length != 1 || genericArguments[0] != typeof(string))
result = string.Format(
"{0}<{1}>",
result,
string.Join(", ",
genericArguments.Select(GetShortName)));
}
return result;
}
public static MethodInfo GetGenericMethod(this Type type, string name, BindingFlags bindingFlags, params Type[] parameterTypes)
{
Func<Type, Type> getGenericTypeDefinition =
t => t.IsGenericParameter ? typeof(object) : t.IsGenericType ? t.GetGenericTypeDefinition() : t;
parameterTypes = parameterTypes.Select(getGenericTypeDefinition).ToArray();
return (from method in type.GetMethods(bindingFlags)
where method.Name == name
let methodParameterTypes =
method.GetParameters().Select(p => getGenericTypeDefinition(p.ParameterType)).ToArray()
where methodParameterTypes.SequenceEqual(parameterTypes)
select method).SingleOrDefault();
}
}
}