BadScript 2
Loading...
Searching...
No Matches
CommandLine.Infrastructure.ReflectionHelper Class Reference

Static Public Member Functions

static void SetAttributeOverride (IEnumerable< Attribute > overrides)
 Assembly attribute overrides for testing.
 
static Maybe< TAttribute > GetAttribute< TAttribute > ()
 
static string GetAssemblyName ()
 
static string GetAssemblyVersion ()
 
static bool IsFSharpOptionType (Type type)
 
static T CreateDefaultImmutableInstance< T > (Type[] constructorTypes)
 
static object CreateDefaultImmutableInstance (Type type, Type[] constructorTypes)
 
static IEnumerable< string > GetNamesOfEnum (Type t)
 

Static Private Member Functions

static Assembly GetExecutingOrEntryAssembly ()
 

Static Private Attributes

static IDictionary< Type, Attribute > _overrides
 Per thread assembly attribute overrides for testing.
 

Detailed Description

Definition at line 14 of file ReflectionHelper.cs.

Member Function Documentation

◆ CreateDefaultImmutableInstance()

static object CommandLine.Infrastructure.ReflectionHelper.CreateDefaultImmutableInstance ( Type  type,
Type[]  constructorTypes 
)
static

Definition at line 101 of file ReflectionHelper.cs.

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 }

◆ CreateDefaultImmutableInstance< T >()

static T CommandLine.Infrastructure.ReflectionHelper.CreateDefaultImmutableInstance< T > ( Type[]  constructorTypes)
static

Definition at line 94 of file ReflectionHelper.cs.

95 {
96 Type t = typeof(T);
97
98 return (T)CreateDefaultImmutableInstance(t, constructorTypes);
99 }
static object CreateDefaultImmutableInstance(Type type, Type[] constructorTypes)

◆ GetAssemblyName()

static string CommandLine.Infrastructure.ReflectionHelper.GetAssemblyName ( )
static

Definition at line 71 of file ReflectionHelper.cs.

72 {
73 Assembly assembly = GetExecutingOrEntryAssembly();
74
75 return assembly.GetName()
76 .Name;
77 }

◆ GetAssemblyVersion()

static string CommandLine.Infrastructure.ReflectionHelper.GetAssemblyVersion ( )
static

Definition at line 79 of file ReflectionHelper.cs.

80 {
81 Assembly assembly = GetExecutingOrEntryAssembly();
82
83 return assembly.GetName()
84 .Version.ToStringInvariant();
85 }

◆ GetAttribute< TAttribute >()

static Maybe< TAttribute > CommandLine.Infrastructure.ReflectionHelper.GetAttribute< TAttribute > ( )
static
Type Constraints
TAttribute :Attribute 

Definition at line 45 of file ReflectionHelper.cs.

46 : 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 }
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 IDictionary< Type, Attribute > _overrides
Per thread assembly attribute overrides for testing.

◆ GetExecutingOrEntryAssembly()

static Assembly CommandLine.Infrastructure.ReflectionHelper.GetExecutingOrEntryAssembly ( )
staticprivate

Definition at line 119 of file ReflectionHelper.cs.

120 {
121 //resolve issues of null EntryAssembly in Xunit Test #392,424,389
122 //return Assembly.GetEntryAssembly();
123 return Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly();
124 }

◆ GetNamesOfEnum()

static IEnumerable< string > CommandLine.Infrastructure.ReflectionHelper.GetNamesOfEnum ( Type  t)
static

Definition at line 126 of file ReflectionHelper.cs.

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 }

◆ IsFSharpOptionType()

static bool CommandLine.Infrastructure.ReflectionHelper.IsFSharpOptionType ( Type  type)
static

Definition at line 87 of file ReflectionHelper.cs.

88 {
89 return type.FullName.StartsWith("Microsoft.FSharp.Core.FSharpOption`1",
90 StringComparison.Ordinal
91 );
92 }

◆ SetAttributeOverride()

static void CommandLine.Infrastructure.ReflectionHelper.SetAttributeOverride ( IEnumerable< Attribute >  overrides)
static

Assembly attribute overrides for testing.

The implementation will fail if two or more attributes of the same type are included in overrides .

Parameters
overridesAttributes that replace the existing assembly attributes or null, to clear any testing attributes.

Definition at line 33 of file ReflectionHelper.cs.

34 {
35 if (overrides != null)
36 {
37 _overrides = overrides.ToDictionary(attr => attr.GetType(), attr => attr);
38 }
39 else
40 {
41 _overrides = null;
42 }
43 }

Member Data Documentation

◆ _overrides

IDictionary<Type, Attribute> CommandLine.Infrastructure.ReflectionHelper._overrides
staticprivate

Per thread assembly attribute overrides for testing.

Definition at line 20 of file ReflectionHelper.cs.


The documentation for this class was generated from the following file: