PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Compilation/BaseServerObjectExpressionBuilder.cs

#
C# | 176 lines | 74 code | 20 blank | 82 comment | 14 complexity | c54df0bbbffb89f4b4dc848c637be559 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <summary>
  3. // Base Server Object Expression Builder
  4. // </summary>
  5. // --------------------------------------------------------------------------------------------------------------------
  6. namespace BlogEngine.Core.Compilation
  7. {
  8. using System;
  9. using System.CodeDom;
  10. using System.ComponentModel;
  11. using System.Web;
  12. using System.Web.Compilation;
  13. using System.Web.UI;
  14. /// <summary>
  15. /// Base Server Object Expression Builder
  16. /// </summary>
  17. public abstract class BaseServerObjectExpressionBuilder : ExpressionBuilder
  18. {
  19. #region Properties
  20. /// <summary>
  21. /// Gets the name of the source object.
  22. /// </summary>
  23. /// <value>The name of the source object.</value>
  24. public abstract string SourceObjectName { get; }
  25. /// <summary>
  26. /// When overridden in a derived class, returns a value indicating whether the current <see cref = "T:System.Web.Compilation.ExpressionBuilder" /> object supports no-compile pages.
  27. /// </summary>
  28. /// <value></value>
  29. /// <returns>true if the <see cref = "T:System.Web.Compilation.ExpressionBuilder" /> supports expression evaluation; otherwise, false.
  30. /// </returns>
  31. public override bool SupportsEvaluate
  32. {
  33. get
  34. {
  35. return true;
  36. }
  37. }
  38. #endregion
  39. #region Public Methods
  40. /// <summary>
  41. /// When overridden in a derived class, returns an object that represents an evaluated expression.
  42. /// </summary>
  43. /// <param name="target">
  44. /// The object containing the expression.
  45. /// </param>
  46. /// <param name="entry">
  47. /// The object that represents information about the property bound to by the expression.
  48. /// </param>
  49. /// <param name="parsedData">
  50. /// The object containing parsed data as returned by <see cref="M:System.Web.Compilation.ExpressionBuilder.ParseExpression(System.String,System.Type,System.Web.Compilation.ExpressionBuilderContext)"/>.
  51. /// </param>
  52. /// <param name="context">
  53. /// Contextual information for the evaluation of the expression.
  54. /// </param>
  55. /// <returns>
  56. /// An object that represents the evaluated expression; otherwise, null if the inheritor does not implement <see cref="M:System.Web.Compilation.ExpressionBuilder.EvaluateExpression(System.Object,System.Web.UI.BoundPropertyEntry,System.Object,System.Web.Compilation.ExpressionBuilderContext)"/>.
  57. /// </returns>
  58. public override object EvaluateExpression(
  59. object target, BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
  60. {
  61. return this.GetRequestedValue(entry.Expression.Trim(), target.GetType(), entry.PropertyInfo.Name);
  62. }
  63. /// <summary>
  64. /// When overridden in a derived class, returns code that is used during page execution to obtain the evaluated expression.
  65. /// </summary>
  66. /// <param name="entry">
  67. /// The object that represents information about the property bound to by the expression.
  68. /// </param>
  69. /// <param name="parsedData">
  70. /// The object containing parsed data as returned by <see cref="M:System.Web.Compilation.ExpressionBuilder.ParseExpression(System.String,System.Type,System.Web.Compilation.ExpressionBuilderContext)"/>.
  71. /// </param>
  72. /// <param name="context">
  73. /// Contextual information for the evaluation of the expression.
  74. /// </param>
  75. /// <returns>
  76. /// A <see cref="T:System.CodeDom.CodeExpression"/> that is used for property assignment.
  77. /// </returns>
  78. public override CodeExpression GetCodeExpression(
  79. BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
  80. {
  81. var inputParams = new CodeExpression[]
  82. {
  83. new CodePrimitiveExpression(entry.Expression.Trim()), new CodeTypeOfExpression(entry.DeclaringType),
  84. new CodePrimitiveExpression(entry.PropertyInfo.Name)
  85. };
  86. // Return a CodeMethodInvokeExpression that will invoke the GetRequestedValue method using the specified input parameters
  87. return new CodeMethodInvokeExpression(
  88. new CodeTypeReferenceExpression(this.GetType()), "Instance().GetRequestedValue", inputParams);
  89. }
  90. /// <summary>
  91. /// Gets the requested value.
  92. /// </summary>
  93. /// <param name="key">
  94. /// The key of the requested value.
  95. /// </param>
  96. /// <param name="targetType">
  97. /// Type of the target.
  98. /// </param>
  99. /// <param name="propertyName">
  100. /// Name of the property.
  101. /// </param>
  102. /// <returns>
  103. /// The requested value.
  104. /// </returns>
  105. public object GetRequestedValue(string key, Type targetType, string propertyName)
  106. {
  107. // First make sure that the server object will be available
  108. if (HttpContext.Current == null)
  109. {
  110. return null;
  111. }
  112. // Get the value
  113. var value = this.GetValue(key);
  114. // Make sure that the value exists
  115. if (value == null)
  116. {
  117. throw new InvalidOperationException(
  118. string.Format("{0} field '{1}' not found.", this.SourceObjectName, key));
  119. }
  120. // If the value is being assigned to a control property we may need to convert it
  121. if (targetType != null)
  122. {
  123. var propDesc = TypeDescriptor.GetProperties(targetType)[propertyName];
  124. // Type mismatch - make sure that the value can be converted
  125. if (propDesc != null && propDesc.PropertyType != value.GetType() && propDesc.Converter != null)
  126. {
  127. if (propDesc.Converter.CanConvertFrom(value.GetType()) == false)
  128. {
  129. throw new InvalidOperationException(
  130. string.Format(
  131. "{0} value '{1}' cannot be converted to type {2}.",
  132. this.SourceObjectName,
  133. key,
  134. propDesc.PropertyType));
  135. }
  136. return propDesc.Converter.ConvertFrom(value);
  137. }
  138. }
  139. // If we reach here, no type mismatch - return the value
  140. return value;
  141. }
  142. #endregion
  143. #region Methods
  144. /// <summary>
  145. /// Gets the value.
  146. /// </summary>
  147. /// <param name="key">
  148. /// The key of the value to retrieve.
  149. /// </param>
  150. /// <returns>
  151. /// The value.
  152. /// </returns>
  153. protected abstract object GetValue(string key);
  154. #endregion
  155. }
  156. }