root / trunk / hammock / src / net35 / Hammock / Extensions / SpecificationExtensions.cs @ 0eea575a
History | View | Annotate | Download (1.2 kB)
1 |
using System; |
---|---|
2 |
using Hammock.Specifications; |
3 |
|
4 |
namespace Hammock.Extensions |
5 |
{ |
6 |
internal static class SpecificationExtensions |
7 |
{ |
8 |
public static bool Satisfies<T>(this object instance) where T : ISpecification |
9 |
{ |
10 |
var marker = Activator.CreateInstance<T>(); |
11 |
var type = typeof (ISpecification<>).MakeGenericType(instance.GetType()); |
12 |
var match = marker.Implements(type); |
13 |
|
14 |
if (!match) |
15 |
{ |
16 |
return false; |
17 |
} |
18 |
|
19 |
var method = type.GetMethod("IsSatisfiedBy"); |
20 |
var result = method.Invoke(marker, new[] {instance}); |
21 |
|
22 |
return (bool) result; |
23 |
} |
24 |
|
25 |
public static bool Satisfies(this object instance, ISpecification specificationType) |
26 |
{ |
27 |
var type = typeof (ISpecification<>).MakeGenericType(instance.GetType()); |
28 |
var match = specificationType.Implements(type); |
29 |
|
30 |
if (!match) |
31 |
{ |
32 |
return false; |
33 |
} |
34 |
|
35 |
var method = type.GetMethod("IsSatisfiedBy"); |
36 |
var result = method.Invoke(specificationType, new[] {instance}); |
37 |
|
38 |
return (bool) result; |
39 |
} |
40 |
} |
41 |
} |