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

NetFabric / NetFabric.CodeAnalysis / 6101088975

06 Sep 2023 06:36PM UTC coverage: 82.932% (+2.0%) from 80.942%
6101088975

push

github

web-flow
Add support for GetEnumerator as an extension method (#26)

* Add support for GetEnumerator as an extension method

* Fixes

224 of 264 branches covered (0.0%)

Branch coverage included in aggregate %.

127 of 127 new or added lines in 16 files covered. (100.0%)

636 of 773 relevant lines covered (82.28%)

17.9 hits per line

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

95.0
/NetFabric.Reflection/Reflection/TypeExtensions.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Diagnostics.CodeAnalysis;
4
using System.Linq;
5
using System.Reflection;
6

7
// ReSharper disable LoopCanBeConvertedToQuery
8

9
namespace NetFabric.Reflection
10
{
11
    public static partial class TypeExtensions
12
    {
13

14
        /// <summary>
15
        /// Gets a value indicating whether <see cref="System.Type"/> implements the given interface <see cref="System.Type"/>.
16
        /// </summary>
17
        /// <param name="type">The <see cref="System.Type"/> to test.</param>
18
        /// <param name="interfaceType">The interface <see cref="System.Type"/> to test.</param>
19
        /// <param name="genericArguments">If methods returns <c>true</c> and interface <see cref="System.Type"/> is generic, contains the generic arguments of the implemented interface.</param>
20
        /// <returns><c>true</c> if <see cref="System.Type"/> implements interface <see cref="System.Type"/>; otherwise, <c>false</c>.</returns>
21
        public static bool ImplementsInterface(this Type type, Type interfaceType, [NotNullWhen(true)] out Type[]? genericArguments)
22
        {
23
            if (!interfaceType.IsGenericType)
133✔
24
            {
25
                genericArguments = Type.EmptyTypes;
100✔
26
                return interfaceType.IsAssignableFrom(type);
100✔
27
            }
28

29
            if (type.IsGenericType && type.GetGenericTypeDefinition() == interfaceType.GetGenericTypeDefinition())
33✔
30
            {
31
                genericArguments = type.GetGenericArguments();
4✔
32
                return true;
4✔
33
            }
34

35
            foreach (var @interface in type.GetAllInterfaces())
82✔
36
            {
37
                if (@interface.IsGenericType && @interface.GetGenericTypeDefinition() == interfaceType.GetGenericTypeDefinition())
17✔
38
                {
39
                    genericArguments = @interface.GetGenericArguments();
10✔
40
                    return true;
10✔
41
                }
42
            }
43

44
            genericArguments = default;
19✔
45
            return false;
19✔
46
        }
10✔
47

48
        static IEnumerable<Type> GetAllInterfaces(this Type type)
49
        {
50
            foreach (var @interface in type.GetInterfaces())
96✔
51
            {
52
                yield return @interface;
17✔
53

54
                foreach (var baseInterface in @interface.GetAllInterfaces())
14!
55
                    yield return baseInterface;
×
56
            }
7✔
57
        }
26✔
58

59
        const BindingFlags PublicInstance =
60
            BindingFlags.Public | BindingFlags.Instance;
61

62
        internal static PropertyInfo? GetPublicInstanceReadProperty(this Type type, string name)
63
        {
64
            foreach (var property in type.GetProperties(PublicInstance))
243✔
65
            {
66
                if (property.Name == name && property.GetGetMethod() is not null)
76✔
67
                    return property;
71✔
68
            }
69

70
            return null;
10✔
71
        }
72

73
        internal static MethodInfo? GetPublicInstanceMethod(this Type type, string name, params Type[] types)
74
            => type.GetMethod(name, PublicInstance, null, types, null);
218✔
75

76
        const BindingFlags PublicInstanceDeclaredOnly =
77
            BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly;
78
        
79
        internal static PropertyInfo? GetPublicInstanceDeclaredOnlyReadProperty(this Type type, string name)
80
            => type.GetProperties(PublicInstanceDeclaredOnly)
33✔
81
                .FirstOrDefault(property => property.Name == name && property.GetGetMethod() is not null);
66!
82
        
83
        internal static MethodInfo? GetPublicInstanceDeclaredOnlyMethod(this Type type, string name, params Type[] types)
84
            => type.GetMethod(name, PublicInstanceDeclaredOnly, null, types, null);
158✔
85

86
        static bool IsByRefLike(this Type type)
87
            => type.IsByRefLike;
95✔
88

89
        public static bool IsSpanOrReadOnlySpan(this Type type)
90
        {
91
            if (type.IsGenericType)
70✔
92
            {
93
                var genericTypeDefinition = type.GetGenericTypeDefinition();
66✔
94
                return genericTypeDefinition == typeof(Span<>) || genericTypeDefinition == typeof(ReadOnlySpan<>);
66✔
95
            }
96

97
            return false;
4✔
98
        }
99
    }
100
}
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