Guest User

Untitled

a guest
Jul 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Text;
  6.  
  7. namespace Test
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. var spec1 = new AdHocSpecification<string>(s => s.StartsWith("a"));
  14. var spec2 = new AdHocSpecification<string>(s => s.EndsWith("a"));
  15.  
  16.  
  17. bool resultado = (spec1 & spec2).Spec().Compile()("adsb");
  18.  
  19. Console.WriteLine(resultado);
  20.  
  21.  
  22. bool resultado2 = (spec1 | spec2).Spec().Compile()("adsb");
  23.  
  24. Console.WriteLine(resultado2);
  25.  
  26.  
  27. bool resultado3 = (!spec1 & spec2).Spec().Compile()("bcdefa");
  28.  
  29. Console.WriteLine(resultado3);
  30.  
  31. Console.ReadLine();
  32. }
  33. }
  34.  
  35.  
  36. public abstract class Specification<T>
  37. {
  38. public abstract Expression<Func<T, bool>> Spec();
  39.  
  40. public static Specification<T> operator &(Specification<T> spec1, Specification<T> spec2)
  41. {
  42. return new AndSpecification<T>(spec1.Spec(), spec2.Spec());
  43. }
  44.  
  45. public static Specification<T> operator |(Specification<T> spec1, Specification<T> spec2)
  46. {
  47. return new OrSpecification<T>(spec1.Spec(), spec2.Spec());
  48. }
  49. public static Specification<T> operator !(Specification<T> spec1)
  50. {
  51. return new NegateSpecification<T>(spec1.Spec());
  52. }
  53. }
  54.  
  55. public class AdHocSpecification<T> :Specification<T>
  56. {
  57. private readonly Expression<Func<T, bool>> _specification;
  58.  
  59. public AdHocSpecification(Expression<Func<T, bool>> specification)
  60. {
  61. _specification = specification;
  62. }
  63.  
  64. public override Expression<Func<T, bool>> Spec()
  65. {
  66. return _specification;
  67. }
  68. }
  69. public class AndSpecification<T> : Specification<T>
  70. {
  71. private readonly Expression<Func<T, bool>> _spec1;
  72. private readonly Expression<Func<T, bool>> _spec2;
  73.  
  74. public AndSpecification(Expression<Func<T, bool>> spec1, Expression<Func<T, bool>> spec2)
  75. {
  76. _spec1 = spec1;
  77. _spec2 = spec2;
  78. }
  79.  
  80. public override Expression<Func<T, bool>> Spec()
  81. {
  82. ParameterExpression param = Expression.Parameter(typeof(T),"x");
  83. return Expression.Lambda<Func<T, bool>>(
  84. Expression.AndAlso(
  85. Expression.Invoke(_spec1, param),
  86. Expression.Invoke(_spec2, param)), param);
  87. }
  88. }
  89.  
  90. public class OrSpecification<T> : Specification<T>
  91. {
  92. private readonly Expression<Func<T, bool>> _spec1;
  93. private readonly Expression<Func<T, bool>> _spec2;
  94.  
  95. public OrSpecification(Expression<Func<T, bool>> spec1, Expression<Func<T, bool>> spec2)
  96. {
  97. _spec1 = spec1;
  98. _spec2 = spec2;
  99. }
  100.  
  101. public override Expression<Func<T, bool>> Spec()
  102. {
  103. ParameterExpression param = Expression.Parameter(typeof(T), "x");
  104. return Expression.Lambda<Func<T, bool>>(
  105. Expression.OrElse(
  106. Expression.Invoke(_spec1, param),
  107. Expression.Invoke(_spec2, param)), param);
  108. }
  109. }
  110.  
  111. public class NegateSpecification<T> : Specification<T>
  112. {
  113. private readonly Expression<Func<T, bool>> _expression;
  114.  
  115. public NegateSpecification(Expression<Func<T, bool>> expression)
  116. {
  117. _expression = expression;
  118. }
  119.  
  120. public override Expression<Func<T, bool>> Spec()
  121. {
  122. ParameterExpression param = Expression.Parameter(typeof(T), "x");
  123. return Expression.Lambda<Func<T, bool>>(Expression.Not(Expression.Invoke(_expression, param)), param);
  124. }
  125. }
  126. }
Add Comment
Please, Sign In to add comment