Skip to content

Commit 31840e7

Browse files
committed
reduce duplicate codes
1 parent da8319a commit 31840e7

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

src/Smdn.Fundamental.Reflection/Smdn.Reflection.Attributes/ICustomAttributeProviderExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,14 @@ internal static bool HasCompilerGeneratedAttribute(this ICustomAttributeProvider
2929
StringComparison.Ordinal
3030
)
3131
);
32+
33+
internal static bool HasIsReadOnlyAttribute(this ICustomAttributeProvider attributeProvider)
34+
=> GetCustomAttributeDataList(attributeProvider)
35+
.Any(static d =>
36+
string.Equals(
37+
d.AttributeType.FullName,
38+
"System.Runtime.CompilerServices.IsReadOnlyAttribute",
39+
StringComparison.Ordinal
40+
)
41+
);
3242
}

src/Smdn.Fundamental.Reflection/Smdn.Reflection/FieldInfoExtensions.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#if NULL_STATE_STATIC_ANALYSIS_ATTRIBUTES
55
using System.Diagnostics.CodeAnalysis;
66
#endif
7-
using System.Linq;
87
using System.Reflection;
98

109
using Smdn.Reflection.Attributes;
@@ -103,7 +102,5 @@ backingField.DeclaringType is null
103102
public static bool IsReadOnly(this FieldInfo f)
104103
=> f is null
105104
? throw new ArgumentNullException(nameof(f))
106-
: f.GetCustomAttributesData().Any(
107-
static d => string.Equals(d.AttributeType.FullName, "System.Runtime.CompilerServices.IsReadOnlyAttribute", StringComparison.Ordinal)
108-
);
105+
: f.HasIsReadOnlyAttribute();
109106
}

src/Smdn.Fundamental.Reflection/Smdn.Reflection/MethodInfoExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using System.Reflection;
99
using System.Runtime.CompilerServices;
1010

11+
using Smdn.Reflection.Attributes;
12+
1113
namespace Smdn.Reflection;
1214

1315
public static class MethodInfoExtensions {
@@ -198,7 +200,5 @@ internal static bool SignatureEqual(MethodInfo x, MethodInfo y)
198200
public static bool IsReadOnly(this MethodInfo m)
199201
=> m is null
200202
? throw new ArgumentNullException(nameof(m))
201-
: m.GetCustomAttributesData().Any(
202-
static d => string.Equals(d.AttributeType.FullName, "System.Runtime.CompilerServices.IsReadOnlyAttribute", StringComparison.Ordinal)
203-
);
203+
: m.HasIsReadOnlyAttribute();
204204
}

src/Smdn.Fundamental.Reflection/Smdn.Reflection/TypeExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using System.Reflection;
77
using System.Runtime.InteropServices;
88

9+
using Smdn.Reflection.Attributes;
10+
911
namespace Smdn.Reflection;
1012

1113
public static class TypeExtensions {
@@ -77,9 +79,7 @@ public static bool IsEnumFlags(this Type t)
7779
public static bool IsReadOnlyValueType(this Type t)
7880
=>
7981
ROCType.IsValueType(t ?? throw new ArgumentNullException(nameof(t))) &&
80-
t.GetCustomAttributesData().Any(
81-
static d => string.Equals(d.AttributeType.FullName, "System.Runtime.CompilerServices.IsReadOnlyAttribute", StringComparison.Ordinal)
82-
);
82+
t.HasIsReadOnlyAttribute();
8383

8484
public static bool IsByRefLikeValueType(this Type t)
8585
=>

0 commit comments

Comments
 (0)