-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy pathReflectionHelper.cs
121 lines (106 loc) · 4.21 KB
/
ReflectionHelper.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
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
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using CommandLine.Core;
using CSharpx;
namespace CommandLine.Infrastructure
{
static class ReflectionHelper
{
/// <summary>
/// Per thread assembly attribute overrides for testing.
/// </summary>
[ThreadStatic] private static IDictionary<Type, Attribute> _overrides;
/// <summary>
/// Assembly attribute overrides for testing.
/// </summary>
/// <remarks>
/// The implementation will fail if two or more attributes of the same type
/// are included in <paramref name="overrides"/>.
/// </remarks>
/// <param name="overrides">
/// Attributes that replace the existing assembly attributes or null,
/// to clear any testing attributes.
/// </param>
public static void SetAttributeOverride(IEnumerable<Attribute> overrides)
{
if (overrides != null)
{
_overrides = overrides.ToDictionary(attr => attr.GetType(), attr => attr);
}
else
{
_overrides = null;
}
}
public static Maybe<TAttribute> GetAttribute<TAttribute>()
where TAttribute : Attribute
{
// Test support
if (_overrides != null)
{
return
_overrides.ContainsKey(typeof(TAttribute)) ?
Maybe.Just((TAttribute)_overrides[typeof(TAttribute)]) :
Maybe.Nothing<TAttribute>();
}
var assembly = GetExecutingOrEntryAssembly();
#if NET40
var attributes = assembly.GetCustomAttributes(typeof(TAttribute), false);
#else
var attributes = assembly.GetCustomAttributes<TAttribute>().ToArray();
#endif
return attributes.Length > 0
? Maybe.Just((TAttribute)attributes[0])
: Maybe.Nothing<TAttribute>();
}
public static string GetAssemblyName()
{
var assembly = GetExecutingOrEntryAssembly();
return assembly.GetName().Name;
}
public static string GetAssemblyVersion()
{
var assembly = GetExecutingOrEntryAssembly();
return assembly.GetName().Version.ToStringInvariant();
}
public static bool IsFSharpOptionType(Type type)
{
return type.FullName.StartsWith(
"Microsoft.FSharp.Core.FSharpOption`1", StringComparison.Ordinal);
}
public static T CreateDefaultImmutableInstance<T>(Type[] constructorTypes)
{
var t = typeof(T);
return (T)CreateDefaultImmutableInstance(t, constructorTypes);
}
public static object CreateDefaultImmutableInstance(Type type, Type[] constructorTypes)
{
var ctor = type.GetTypeInfo().GetConstructor(constructorTypes);
if (ctor == null)
{
throw new InvalidOperationException($"Type {type.FullName} appears to be immutable, but no constructor found to accept values.");
}
var values = (from prms in ctor.GetParameters()
select prms.ParameterType.CreateDefaultForImmutable()).ToArray();
return ctor.Invoke(values);
}
private static Assembly GetExecutingOrEntryAssembly()
{
//resolve issues of null EntryAssembly in Xunit Test #392,424,389
//return Assembly.GetEntryAssembly();
return Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly();
}
public static IEnumerable<string> GetNamesOfEnum(Type t)
{
if (t.IsEnum)
return Enum.GetNames(t);
Type u = Nullable.GetUnderlyingType(t);
if (u != null && u.IsEnum)
return Enum.GetNames(u);
return Enumerable.Empty<string>();
}
}
}