BadScript 2
Loading...
Searching...
No Matches
ReflectionHelper.cs
Go to the documentation of this file.
1// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2
3using System;
4using System.Collections.Generic;
5using System.Linq;
6using System.Reflection;
7
9
10using CSharpx;
11
13{
14 internal static class ReflectionHelper
15 {
19 [ThreadStatic]
20 private static IDictionary<Type, Attribute> _overrides;
21
33 public static void SetAttributeOverride(IEnumerable<Attribute> overrides)
34 {
35 if (overrides != null)
36 {
37 _overrides = overrides.ToDictionary(attr => attr.GetType(), attr => attr);
38 }
39 else
40 {
41 _overrides = null;
42 }
43 }
44
46 where TAttribute : Attribute
47 {
48 // Test support
49 if (_overrides != null)
50 {
51 return
52 _overrides.ContainsKey(typeof(TAttribute))
53 ? Maybe.Just((TAttribute)_overrides[typeof(TAttribute)])
54 : Maybe.Nothing<TAttribute>();
55 }
56
57 Assembly assembly = GetExecutingOrEntryAssembly();
58
59#if NET40
60 object[] attributes = assembly.GetCustomAttributes(typeof(TAttribute), false);
61#else
62 TAttribute[] attributes = assembly.GetCustomAttributes<TAttribute>()
63 .ToArray();
64#endif
65
66 return attributes.Length > 0
67 ? Maybe.Just((TAttribute)attributes[0])
68 : Maybe.Nothing<TAttribute>();
69 }
70
71 public static string GetAssemblyName()
72 {
73 Assembly assembly = GetExecutingOrEntryAssembly();
74
75 return assembly.GetName()
76 .Name;
77 }
78
79 public static string GetAssemblyVersion()
80 {
81 Assembly assembly = GetExecutingOrEntryAssembly();
82
83 return assembly.GetName()
84 .Version.ToStringInvariant();
85 }
86
87 public static bool IsFSharpOptionType(Type type)
88 {
89 return type.FullName.StartsWith("Microsoft.FSharp.Core.FSharpOption`1",
90 StringComparison.Ordinal
91 );
92 }
93
94 public static T CreateDefaultImmutableInstance<T>(Type[] constructorTypes)
95 {
96 Type t = typeof(T);
97
98 return (T)CreateDefaultImmutableInstance(t, constructorTypes);
99 }
100
101 public static object CreateDefaultImmutableInstance(Type type, Type[] constructorTypes)
102 {
103 ConstructorInfo ctor = type.GetTypeInfo()
104 .GetConstructor(constructorTypes);
105
106 if (ctor == null)
107 {
108 throw new
109 InvalidOperationException($"Type {type.FullName} appears to be immutable, but no constructor found to accept values."
110 );
111 }
112
113 object[] values = (from prms in ctor.GetParameters()
114 select prms.ParameterType.CreateDefaultForImmutable()).ToArray();
115
116 return ctor.Invoke(values);
117 }
118
119 private static Assembly GetExecutingOrEntryAssembly()
120 {
121 //resolve issues of null EntryAssembly in Xunit Test #392,424,389
122 //return Assembly.GetEntryAssembly();
123 return Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly();
124 }
125
126 public static IEnumerable<string> GetNamesOfEnum(Type t)
127 {
128 if (t.IsEnum)
129 {
130 return Enum.GetNames(t);
131 }
132
133 Type u = Nullable.GetUnderlyingType(t);
134
135 if (u != null && u.IsEnum)
136 {
137 return Enum.GetNames(u);
138 }
139
140 return Enumerable.Empty<string>();
141 }
142 }
143}
The Maybe type models an optional value. A value of type Maybe a either contains a value of type a (r...
Definition Maybe.cs:33
static void SetAttributeOverride(IEnumerable< Attribute > overrides)
Assembly attribute overrides for testing.
static IEnumerable< string > GetNamesOfEnum(Type t)
static IDictionary< Type, Attribute > _overrides
Per thread assembly attribute overrides for testing.
static object CreateDefaultImmutableInstance(Type type, Type[] constructorTypes)
static T CreateDefaultImmutableInstance< T >(Type[] constructorTypes)
static Maybe< TAttribute > GetAttribute< TAttribute >()