Skip to content

Commit 4c3f6e3

Browse files
committed
Adding PropertyInfo.IsReadable() and .IsWritable() / Adding MemberInfo.HasAttribute<TAttribute>()
1 parent 0041b3b commit 4c3f6e3

File tree

9 files changed

+142
-0
lines changed

9 files changed

+142
-0
lines changed

NetStandardPolyfills.UnitTests.Net40/WhenRetrievingTypeMembers.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ namespace AgileObjects.NetStandardPolyfills.UnitTests.Net40
55
[TestFixture]
66
public class WhenRetrievingTypeMembers : MemberTestsBase
77
{
8+
[Test]
9+
public override void ShouldFlagAMemberWithAnAttribute() => DoShouldFlagAMemberWithAnAttribute();
10+
11+
[Test]
12+
public override void ShouldFlagAMemberWithoutAnAttribute() => DoShouldFlagAMemberWithoutAnAttribute();
13+
814
[Test]
915
public override void ShouldRetrievePublicStaticMembers() => DoShouldRetrievePublicStaticMembers();
1016

NetStandardPolyfills.UnitTests.Net40/WhenRetrievingTypeProperties.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ namespace AgileObjects.NetStandardPolyfills.UnitTests.Net40
55
[TestFixture]
66
public class WhenRetrievingTypeProperties : PropertyTestsBase
77
{
8+
[Test]
9+
public override void ShouldFlagAReadableProperty() => DoShouldFlagAReadableProperty();
10+
11+
[Test]
12+
public override void ShouldFlagANonReadableProperty() => DoShouldFlagANonReadableProperty();
13+
14+
[Test]
15+
public override void ShouldFlagAWritableProperty() => DoShouldFlagAWritableProperty();
16+
17+
[Test]
18+
public override void ShouldFlagANonWritableProperty() => DoShouldFlagANonWritableProperty();
19+
820
[Test]
921
public override void ShouldRetrievePublicStaticProperties() => DoShouldRetrievePublicStaticProperties();
1022

NetStandardPolyfills.UnitTests.NetCore/WhenRetrievingTypeMembers.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ namespace AgileObjects.NetStandardPolyfills.UnitTests.NetCore
44

55
public class WhenRetrievingTypeMembers : MemberTestsBase
66
{
7+
[Fact]
8+
public override void ShouldFlagAMemberWithAnAttribute() => DoShouldFlagAMemberWithAnAttribute();
9+
10+
[Fact]
11+
public override void ShouldFlagAMemberWithoutAnAttribute() => DoShouldFlagAMemberWithoutAnAttribute();
12+
713
[Fact]
814
public override void ShouldRetrievePublicStaticMembers() => DoShouldRetrievePublicStaticMembers();
915

NetStandardPolyfills.UnitTests.NetCore/WhenRetrievingTypeProperties.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ namespace AgileObjects.NetStandardPolyfills.UnitTests.NetCore
44

55
public class WhenRetrievingTypeProperties : PropertyTestsBase
66
{
7+
[Fact]
8+
public override void ShouldFlagAReadableProperty() => DoShouldFlagAReadableProperty();
9+
10+
[Fact]
11+
public override void ShouldFlagANonReadableProperty() => DoShouldFlagANonReadableProperty();
12+
13+
[Fact]
14+
public override void ShouldFlagAWritableProperty() => DoShouldFlagAWritableProperty();
15+
16+
[Fact]
17+
public override void ShouldFlagANonWritableProperty() => DoShouldFlagANonWritableProperty();
18+
719
[Fact]
820
public override void ShouldRetrievePublicStaticProperties() => DoShouldRetrievePublicStaticProperties();
921

NetStandardPolyfills.UnitTests/MemberTestsBase.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ namespace AgileObjects.NetStandardPolyfills.UnitTests
66

77
public abstract class MemberTestsBase
88
{
9+
public abstract void ShouldFlagAMemberWithAnAttribute();
10+
11+
public abstract void ShouldFlagAMemberWithoutAnAttribute();
12+
913
public abstract void ShouldRetrievePublicStaticMembers();
1014

1115
public abstract void ShouldRetrievePublicStaticMembersByName();
@@ -40,6 +44,22 @@ public abstract class MemberTestsBase
4044

4145
#region Test Implementations
4246

47+
protected void DoShouldFlagAMemberWithAnAttribute()
48+
{
49+
typeof(TestHelper)
50+
.GetPublicInstanceMember(nameof(TestHelper.PublicInstanceProperty))
51+
.HasAttribute<MyAttribute>()
52+
.ShouldBeTrue();
53+
}
54+
55+
protected void DoShouldFlagAMemberWithoutAnAttribute()
56+
{
57+
typeof(TestHelper)
58+
.GetPublicStaticMember(nameof(TestHelper.PublicStaticProperty))
59+
.HasAttribute<MyAttribute>()
60+
.ShouldBeFalse();
61+
}
62+
4363
protected void DoShouldRetrievePublicStaticMembers()
4464
{
4565
var members = typeof(TestHelper)

NetStandardPolyfills.UnitTests/PropertyTestsBase.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ namespace AgileObjects.NetStandardPolyfills.UnitTests
55

66
public abstract class PropertyTestsBase
77
{
8+
public abstract void ShouldFlagAReadableProperty();
9+
10+
public abstract void ShouldFlagANonReadableProperty();
11+
12+
public abstract void ShouldFlagAWritableProperty();
13+
14+
public abstract void ShouldFlagANonWritableProperty();
15+
816
public abstract void ShouldRetrievePublicStaticProperties();
917

1018
public abstract void ShouldRetrieveAPublicStaticPropertyByName();
@@ -49,6 +57,38 @@ public abstract class PropertyTestsBase
4957

5058
#region Test Implementations
5159

60+
protected void DoShouldFlagAReadableProperty()
61+
{
62+
typeof(TestHelper)
63+
.GetPublicInstanceProperty(nameof(TestHelper.PublicInstanceProperty))
64+
.IsReadable()
65+
.ShouldBeTrue();
66+
}
67+
68+
protected void DoShouldFlagANonReadableProperty()
69+
{
70+
typeof(TestHelper)
71+
.GetPublicInstanceProperty(nameof(TestHelper.PublicWriteOnlyProperty))
72+
.IsReadable()
73+
.ShouldBeFalse();
74+
}
75+
76+
protected void DoShouldFlagAWritableProperty()
77+
{
78+
typeof(TestHelper)
79+
.GetPublicInstanceProperty(nameof(TestHelper.PublicInstanceProperty))
80+
.IsWritable()
81+
.ShouldBeTrue();
82+
}
83+
84+
protected void DoShouldFlagANonWritableProperty()
85+
{
86+
typeof(TestHelper)
87+
.GetPublicInstanceProperty(nameof(TestHelper.PublicReadOnlyProperty))
88+
.IsWritable()
89+
.ShouldBeFalse();
90+
}
91+
5292
protected void DoShouldRetrievePublicStaticProperties()
5393
{
5494
typeof(TestHelper)

NetStandardPolyfills.UnitTests/TestClasses/TestHelper.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class TestHelper
1414
internal int NonPublicInstanceField = 0;
1515
internal static int NonPublicStaticField = 0;
1616
private readonly Dictionary<string, string> _dictionary;
17+
private int _value;
1718

1819
public TestHelper()
1920
{
@@ -32,8 +33,16 @@ protected TestHelper(string something)
3233
public static explicit operator int[] (TestHelper testHelper) => new[] { 1, 2, 3 };
3334
public static explicit operator TestHelper(long value) => new TestHelper();
3435

36+
[My]
3537
public int PublicInstanceProperty { get; set; }
3638

39+
public int PublicReadOnlyProperty => _value;
40+
41+
public int PublicWriteOnlyProperty
42+
{
43+
set => _value = value;
44+
}
45+
3746
public static int PublicStaticProperty { get; set; }
3847

3948
internal int NonPublicInstanceProperty { get; set; }

NetStandardPolyfills/MemberExtensionsPolyfill.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,29 @@
1111
/// </summary>
1212
public static class MemberExtensionsPolyfill
1313
{
14+
/// <summary>
15+
/// Returns a value indicating if the <paramref name="memberInfo"/> has the Attribute given
16+
/// in the <typeparamref name="TAttribute"/> type argument.
17+
/// </summary>
18+
/// <typeparam name="TAttribute">The Attribute type for which to make the determination.</typeparam>
19+
/// <param name="memberInfo">The MemberInfo for which to make the determination.</param>
20+
/// <returns>
21+
/// True if the <paramref name="memberInfo"/> has the given <typeparamref name="TAttribute"/>,
22+
/// otherwise false.
23+
/// </returns>
24+
public static bool HasAttribute<TAttribute>(this MemberInfo memberInfo)
25+
{
26+
#if NET_STANDARD
27+
return memberInfo
28+
.CustomAttributes
29+
.Any(a => a.AttributeType == typeof(TAttribute));
30+
#else
31+
return memberInfo
32+
.GetCustomAttributes(typeof(TAttribute), inherit: false)
33+
.Any();
34+
#endif
35+
}
36+
1437
/// <summary>
1538
/// Gets the public, static-scoped members for the given <paramref name="type"/>.
1639
/// </summary>

NetStandardPolyfills/PropertyExtensionsPolyfill.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@
1313
/// </summary>
1414
public static class PropertyExtensionsPolyfill
1515
{
16+
/// <summary>
17+
/// Returns a value indicating whether the <paramref name="property"/> is readable.
18+
/// </summary>
19+
/// <param name="property">The property for which to make the determination.</param>
20+
/// <returns>True if the <paramref name="property"/> is readable, otherwise false.</returns>
21+
public static bool IsReadable(this PropertyInfo property) => property.GetGetter() != null;
22+
23+
/// <summary>
24+
/// Returns a value indicating whether the <paramref name="property"/> is writable.
25+
/// </summary>
26+
/// <param name="property">The property for which to make the determination.</param>
27+
/// <returns>True if the <paramref name="property"/> is writable, otherwise false.</returns>
28+
public static bool IsWritable(this PropertyInfo property) => property.GetSetter() != null;
29+
1630
/// <summary>
1731
/// Gets the public, static-scoped properties for the given <paramref name="type"/>.
1832
/// </summary>

0 commit comments

Comments
 (0)