• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

luttje / Key2Joy / 6602636543

22 Oct 2023 08:16AM UTC coverage: 44.104% (-8.4%) from 52.519%
6602636543

Pull #50

github

web-flow
Merge cf342a7b3 into 14b7ce9a7
Pull Request #50: Add XInput in preparation for gamepad triggers + add xmldoc

764 of 2383 branches covered (0.0%)

Branch coverage included in aggregate %.

3060 of 3060 new or added lines in 106 files covered. (100.0%)

3896 of 8183 relevant lines covered (47.61%)

15812.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

14.57
/Core/Key2Joy.Core/Util/TypeExtensions.cs
1
using System;
2
using System.Collections.Generic;
3

4
namespace Key2Joy.Util;
5

6
public static class TypeExtensions
7
{
8
    /// <summary>
9
    /// Source: https://stackoverflow.com/a/457708
10
    /// </summary>
11
    /// <param name="generic"></param>
12
    /// <param name="toCheck"></param>
13
    /// <returns></returns>
14
    public static bool IsSubclassOfRawGeneric(this Type generic, Type toCheck)
15
    {
16
        while (toCheck != null && toCheck != typeof(object))
4✔
17
        {
18
            var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
2!
19
            if (generic == cur)
2✔
20
            {
21
                return true;
1✔
22
            }
23
            toCheck = toCheck.BaseType;
1✔
24
        }
25
        return false;
2✔
26
    }
27

28
    public static bool IsList(this Type generic) => generic.IsGenericType && (generic.GetGenericTypeDefinition() == typeof(List<>));
8✔
29

30
    /// <summary>
31
    /// Copies a given array to a new array of the target type, e.g: from object[] to string[].
32
    /// </summary>
33
    /// <param name="originalArray"></param>
34
    /// <param name="elementType"></param>
35
    /// <returns></returns>
36
    public static Array CopyArrayToNewType(this object[] originalArray, Type elementType)
37
    {
38
        var newArray = Array.CreateInstance(elementType, originalArray.Length);
7✔
39

40
        for (var i = 0; i < originalArray.Length; i++)
48✔
41
        {
42
            newArray.SetValue(Convert.ChangeType(originalArray[i], elementType), i);
17✔
43
        }
44

45
        return newArray;
7✔
46

47
        // Explored alternatives:
48
        // B: This doesn't actually cast it tot he correct type (but leaves it an object[]
49
        //var convertedArray = (Array)Array.ConvertAll(originalArray, item => Convert.ChangeType(item, elementType));
50

51
        //return convertedArray;
52

53
        // C: Dynamically invoke Array.ConvertAll so that it returns an array of type elementType[]
54
        //var convertAllMethod = typeof(Array).GetMethod("ConvertAll", BindingFlags.Public | BindingFlags.Static);
55
        //var genericConvertAllMethod = convertAllMethod.MakeGenericMethod(typeof(object), elementType);
56
        //var converterDelegateType = typeof(Func<,>).MakeGenericType(typeof(object), elementType);
57

58
        //return (Array)genericConvertAllMethod.Invoke(
59
        //    null,
60
        //    new object[] {
61
        //        originalArray,
62
        //        Delegate.CreateDelegate(converterDelegateType, typeof(Convert).GetMethod("ChangeType", new[] { typeof(object), typeof(Type) }))
63
        //    });
64
    }
65

66
    /// <summary>
67
    /// Gets the minimum value for a numeric type
68
    /// </summary>
69
    /// <param name="type"></param>
70
    /// <returns></returns>
71
    public static object GetNumericMinValue(this Type type)
72
    {
73
        type = Nullable.GetUnderlyingType(type) ?? type;
×
74

75
        if (type == typeof(int))
×
76
        {
77
            return int.MinValue;
×
78
        }
79
        else if (type == typeof(uint))
×
80
        {
81
            return uint.MinValue;
×
82
        }
83
        else if (type == typeof(long))
×
84
        {
85
            return long.MinValue;
×
86
        }
87
        else if (type == typeof(ulong))
×
88
        {
89
            return ulong.MinValue;
×
90
        }
91
        else if (type == typeof(short))
×
92
        {
93
            return short.MinValue;
×
94
        }
95
        else if (type == typeof(ushort))
×
96
        {
97
            return ushort.MinValue;
×
98
        }
99
        else if (type == typeof(byte))
×
100
        {
101
            return byte.MinValue;
×
102
        }
103
        else if (type == typeof(sbyte))
×
104
        {
105
            return sbyte.MinValue;
×
106
        }
107
        else if (type == typeof(float))
×
108
        {
109
            return float.MinValue;
×
110
        }
111
        else if (type == typeof(double))
×
112
        {
113
            return double.MinValue;
×
114
        }
115
        else if (type == typeof(decimal))
×
116
        {
117
            return decimal.MinValue;
×
118
        }
119
        else
120
        {
121
            throw new ArgumentException($"Type {type} is not a numeric type");
×
122
        }
123
    }
124

125
    /// <summary>
126
    /// Gets the maximum value for a numeric type
127
    /// </summary>
128
    /// <param name="type"></param>
129
    /// <returns></returns>
130
    public static object GetNumericMaxValue(this Type type)
131
    {
132
        type = Nullable.GetUnderlyingType(type) ?? type;
×
133

134
        if (type == typeof(int))
×
135
        {
136
            return int.MaxValue;
×
137
        }
138
        else if (type == typeof(uint))
×
139
        {
140
            return uint.MaxValue;
×
141
        }
142
        else if (type == typeof(long))
×
143
        {
144
            return long.MaxValue;
×
145
        }
146
        else if (type == typeof(ulong))
×
147
        {
148
            return ulong.MaxValue;
×
149
        }
150
        else if (type == typeof(short))
×
151
        {
152
            return short.MaxValue;
×
153
        }
154
        else if (type == typeof(ushort))
×
155
        {
156
            return ushort.MaxValue;
×
157
        }
158
        else if (type == typeof(byte))
×
159
        {
160
            return byte.MaxValue;
×
161
        }
162
        else if (type == typeof(sbyte))
×
163
        {
164
            return sbyte.MaxValue;
×
165
        }
166
        else if (type == typeof(float))
×
167
        {
168
            return float.MaxValue;
×
169
        }
170
        else if (type == typeof(double))
×
171
        {
172
            return double.MaxValue;
×
173
        }
174
        else if (type == typeof(decimal))
×
175
        {
176
            return decimal.MaxValue;
×
177
        }
178
        else
179
        {
180
            throw new ArgumentException($"Type {type} is not a numeric type");
×
181
        }
182
    }
183

184
    /// <summary>
185
    ///
186
    /// </summary>
187
    /// <param name="input"></param>
188
    /// <returns></returns>
189
    public static decimal ToDecimalSafe(object input)
190
    {
191
        if (input is decimal dec)
×
192
        {
193
            return dec;
×
194
        }
195

196
        if (input is double d)
×
197
        {
198
            if (d > (double)decimal.MaxValue)
×
199
            {
200
                return decimal.MaxValue;
×
201
            }
202
            else if (d < (double)decimal.MinValue)
×
203
            {
204
                return decimal.MinValue;
×
205
            }
206
            else
207
            {
208
                return (decimal)d;
×
209
            }
210
        }
211

212
        if (input is float f)
×
213
        {
214
            if (f > (float)decimal.MaxValue)
×
215
            {
216
                return decimal.MaxValue;
×
217
            }
218
            else if (f < (float)decimal.MinValue)
×
219
            {
220
                return decimal.MinValue;
×
221
            }
222
            else
223
            {
224
                return (decimal)f;
×
225
            }
226
        }
227

228
        try
229
        {
230
            return Convert.ToDecimal(input);
×
231
        }
232
        catch (OverflowException)
×
233
        {
234
            return decimal.MaxValue;
×
235
        }
236
    }
×
237
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc