/src/MvcContrib.BrailViewEngine/IgnoreNull.cs

https://github.com/JasonTrue/MvcContrib · C# · 98 lines · 70 code · 14 blank · 14 comment · 20 complexity · ba3d3ee18e4ba48a6a765bc56d519cc0 MD5 · raw file

  1. // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // MODIFICATIONS HAVE BEEN MADE TO THIS FILE
  15. namespace MvcContrib.BrailViewEngine
  16. {
  17. using System.Collections.Generic;
  18. using Boo.Lang;
  19. public class IgnoreNull : IQuackFu
  20. {
  21. private readonly object target;
  22. public IgnoreNull(object target)
  23. {
  24. this.target = target;
  25. }
  26. #region IQuackFu Members
  27. public object QuackGet(string name, object[] parameters)
  28. {
  29. if (name == "_IsIgnoreNullReferencingNotNullObject_")
  30. return target != null;
  31. if (target == null)
  32. return this;
  33. object value;
  34. if (IsNullOrEmpty(parameters))
  35. value = ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.GetProperty(target, name);
  36. else
  37. value = ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.GetSlice(target, name, parameters);
  38. return new IgnoreNull(value);
  39. }
  40. public object QuackSet(string name, object[] parameters, object obj)
  41. {
  42. if (target == null)
  43. return this;
  44. if (IsNullOrEmpty(parameters))
  45. ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.SetProperty(target, name, obj);
  46. else
  47. ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.SetSlice(target, name,
  48. GetParameterArray(parameters, obj));
  49. return this;
  50. }
  51. public object QuackInvoke(string name, object[] args)
  52. {
  53. if (target == null)
  54. return this;
  55. object value = ExpandDuckTypedExpressions_WorkaroundForDuplicateVirtualMethods.Invoke(target, name, args);
  56. return new IgnoreNull(value);
  57. }
  58. #endregion
  59. private static bool IsNullOrEmpty(object[] parameters)
  60. {
  61. return parameters == null || parameters.Length == 0;
  62. }
  63. private static object[] GetParameterArray(object[] parameters, object obj)
  64. {
  65. return new List<object>(parameters) {obj}.ToArray();
  66. }
  67. public override string ToString()
  68. {
  69. if (target == null)
  70. return string.Empty;
  71. return target.ToString();
  72. }
  73. public static bool AreEqual(object left, object right)
  74. {
  75. var temp = left as IgnoreNull;
  76. if (temp != null)
  77. left = temp.target;
  78. temp = right as IgnoreNull;
  79. if (temp != null)
  80. right = temp.target;
  81. return Equals(left, right);
  82. }
  83. }
  84. }