-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
import CodeQuery project files instead of separate library
- Loading branch information
1 parent
8172456
commit 364711a
Showing
11 changed files
with
500 additions
and
4 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
using JetBrains.Annotations; | ||
|
||
namespace CodeQuery | ||
{ | ||
public static class FieldInfoExtensions | ||
{ | ||
[NotNull] | ||
public static IEnumerable<FieldInfo> ThatAreStatic([NotNull] this IEnumerable<FieldInfo> items) | ||
{ | ||
return items.Where(x => x.IsStatic); | ||
} | ||
|
||
[NotNull] | ||
public static IEnumerable<T> CustomAttributesOfType<T>([NotNull] this FieldInfo input) where T : Attribute | ||
{ | ||
return ((MemberInfo)input).CustomAttributesOfType<T>(); | ||
} | ||
|
||
public static bool HasAttributeOfType<TAttributeType>([NotNull] this FieldInfo input) where TAttributeType : Attribute | ||
{ | ||
return input.CustomAttributesOfType<TAttributeType>().Any(); | ||
} | ||
|
||
[NotNull] | ||
public static IEnumerable<FieldInfo> WithAttributeOfType<TAttributeType>([NotNull] this IEnumerable<FieldInfo> input) where TAttributeType : Attribute | ||
{ | ||
return input.Where(x => x.HasAttributeOfType<TAttributeType>()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
using JetBrains.Annotations; | ||
|
||
namespace CodeQuery | ||
{ | ||
internal static class MemberInfoExtensions | ||
{ | ||
[NotNull] | ||
internal static IEnumerable<T> CustomAttributesOfType<T>([NotNull] this MemberInfo input) where T : Attribute | ||
{ | ||
return input.GetCustomAttributes(typeof(T), true).Cast<T>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
using JetBrains.Annotations; | ||
|
||
namespace CodeQuery | ||
{ | ||
public static class PropertyInfoExtensions | ||
{ | ||
[NotNull] | ||
public static IEnumerable<Attribute> CustomAttributes([NotNull] this PropertyInfo input) | ||
{ | ||
return input.GetCustomAttributes(true).Cast<Attribute>(); | ||
} | ||
|
||
[NotNull] | ||
public static IEnumerable<T> CustomAttributesOfType<T>([NotNull] this PropertyInfo input) where T : Attribute | ||
{ | ||
return ((MemberInfo)input).CustomAttributesOfType<T>(); | ||
} | ||
|
||
public static bool HasAttributeOfType<TAttributeType>([NotNull] this PropertyInfo input) where TAttributeType : Attribute | ||
{ | ||
return input.CustomAttributesOfType<TAttributeType>().Any(); | ||
} | ||
|
||
[NotNull] | ||
public static IEnumerable<PropertyInfo> ThatHaveAGetter([NotNull] this IEnumerable<PropertyInfo> input) | ||
{ | ||
return input.Where(x => x.CanRead); | ||
} | ||
|
||
[NotNull] | ||
public static IEnumerable<PropertyInfo> ThatHaveASetter([NotNull] this IEnumerable<PropertyInfo> input) | ||
{ | ||
return input.Where(x => x.CanWrite); | ||
} | ||
|
||
[NotNull] | ||
public static IEnumerable<PropertyInfo> WithAttributeOfType<TAttributeType>([NotNull] this IEnumerable<PropertyInfo> input) where TAttributeType : Attribute | ||
{ | ||
return input.Where(x => x.HasAttributeOfType<TAttributeType>()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
|
||
namespace CodeQuery | ||
{ | ||
public static class TypeExtensions | ||
{ | ||
public static bool IsGenericAssignableFrom(this Type target, Type source) | ||
{ | ||
if (!target.IsGenericType) | ||
{ | ||
return false; | ||
} | ||
|
||
if (target.IsAssignableFrom(source)) | ||
{ | ||
return true; | ||
} | ||
|
||
if (!typeof(Nullable<>).IsAssignableFrom(target)) | ||
{ | ||
return false; | ||
} | ||
|
||
var genericParameters = target.GetGenericArguments(); | ||
|
||
return genericParameters[0].IsAssignableFrom(source); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/MvbaCoreTests/CodeQuery/FieldInfoExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using System.ComponentModel; | ||
using System.Linq; | ||
|
||
using CodeQuery; | ||
|
||
using FluentAssert; | ||
|
||
using NUnit.Framework; | ||
|
||
namespace CodeQueryTests | ||
{ | ||
public class FieldInfoExtensionsTests | ||
{ | ||
[TestFixture] | ||
public class When_asked_for_the_static_FieldInfos | ||
{ | ||
[Test] | ||
public void Should_return_only_the_static_ones() | ||
{ | ||
var fields = typeof(TestClass).GetFields(); | ||
fields.Length.ShouldBeEqualTo(2); | ||
var fieldInfos = fields.ThatAreStatic(); | ||
fieldInfos.Count().ShouldBeEqualTo(1); | ||
} | ||
|
||
public class TestClass | ||
{ | ||
public static int Foo = 7; | ||
public int Bar = 6; | ||
} | ||
} | ||
|
||
[TestFixture] | ||
public class When_asked_if_there_are_any_custom_Attributes_of_a_specific_type_for_a_FieldInfo | ||
{ | ||
[Test] | ||
public void Should_return_False_if_there_are_no_matching_attributes() | ||
{ | ||
var hasAttributeOfType = typeof(TestClass).GetField("Id").HasAttributeOfType<TestAttribute>(); | ||
hasAttributeOfType.ShouldBeFalse(); | ||
} | ||
|
||
[Test] | ||
public void Should_return_True_if_there_are_matching_attributes() | ||
{ | ||
var hasAttributeOfType = typeof(TestClass).GetField("Id").HasAttributeOfType<ReadOnlyAttribute>(); | ||
hasAttributeOfType.ShouldBeTrue(); | ||
} | ||
|
||
public class TestClass | ||
{ | ||
[ReadOnly(true)] | ||
public int Id; | ||
} | ||
} | ||
|
||
[TestFixture] | ||
public class When_asked_to_get_custom_Attributes_of_a_specific_type_for_a_FieldInfo | ||
{ | ||
[Test] | ||
public void Should_get_the_matching_Attributes() | ||
{ | ||
var attributes = typeof(TestClass).GetField("Id").CustomAttributesOfType<ReadOnlyAttribute>(); | ||
attributes.Count().ShouldBeEqualTo(1); | ||
} | ||
|
||
[Test] | ||
public void Should_return_an_empty_container_if_there_are_no_matching_Attributes() | ||
{ | ||
var attributes = typeof(TestClass).GetField("Id").CustomAttributesOfType<TestAttribute>(); | ||
attributes.ShouldNotBeNull(); | ||
attributes.Count().ShouldBeEqualTo(0); | ||
} | ||
|
||
public class TestClass | ||
{ | ||
[ReadOnly(true)] | ||
public int Id; | ||
} | ||
} | ||
|
||
[TestFixture] | ||
public class When_asked_to_get_FieldInfos_that_have_custom_Attributes_of_a_specific_type | ||
{ | ||
[Test] | ||
public void Should_get_an_empty_container_if_there_are_no_matching_Fields() | ||
{ | ||
var fieldInfos = typeof(TestClass).GetFields().WithAttributeOfType<TestAttribute>(); | ||
fieldInfos.ShouldNotBeNull(); | ||
fieldInfos.Count().ShouldBeEqualTo(0); | ||
} | ||
|
||
[Test] | ||
public void Should_get_the_matching_FieldInfos() | ||
{ | ||
var fieldInfos = typeof(TestClass).GetFields().WithAttributeOfType<ReadOnlyAttribute>(); | ||
fieldInfos.Count().ShouldBeEqualTo(1); | ||
fieldInfos.First().Name.ShouldBeEqualTo("Id"); | ||
} | ||
|
||
public class TestClass | ||
{ | ||
[ReadOnly(true)] | ||
public int Id; | ||
|
||
public string Name; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.