UserFormCodeVaildator.cs
上传用户:husern
上传日期:2022-03-24
资源大小:534k
文件大小:7k
源码类别:

编辑器/阅读器

开发平台:

C#

  1. // -- FILE ------------------------------------------------------------------
  2. // name       : UserFormCodeValidator.cs
  3. // project    : Itenso Web User Forms
  4. // created    : Jani Giannoudis - 2008.10.30
  5. // language   : c#
  6. // environment: .NET 2.0
  7. // copyright  : (c) 2008 by Itenso GmbH, Switzerland
  8. // --------------------------------------------------------------------------
  9. using System;
  10. using System.Reflection;
  11. using System.Collections.Generic;
  12. using System.Web.UI;
  13. using System.Text;
  14. namespace Itenso.WebUserForms.Controls
  15. {
  16. // ------------------------------------------------------------------------
  17. public class UserFormCodeValidator
  18. {
  19. // ----------------------------------------------------------------------
  20. public UserFormCodeValidator()
  21. {
  22. InitializeExcludedMethods();
  23. InitializeExcludedFields();
  24. } // UserFormCodeValidator
  25. // ----------------------------------------------------------------------
  26. public List<string> ExcludedMethods
  27. {
  28. get { return this.excludedMethods; }
  29. } // ExcludedMethods
  30. // ----------------------------------------------------------------------
  31. public List<string> ExcludedFields
  32. {
  33. get { return this.excludedFields; }
  34. } // ExcludedFields
  35. // ----------------------------------------------------------------------
  36. public bool ExcludeControlFields
  37. {
  38. get { return this.excludeControlFields; }
  39. } // ExcludeControlFields
  40. // ----------------------------------------------------------------------
  41. public virtual void Validate( UserControl userControl )
  42. {
  43. ValidateMethods( userControl );
  44. ValidateFields( userControl );
  45. } // ValidateForm
  46. // ----------------------------------------------------------------------
  47. public void InitializeExcludedMethods()
  48. {
  49. this.excludedMethods.Clear();
  50. this.excludedMethods.Add( "__BuildControl*" );
  51. this.excludedMethods.Add( "__DataBind*" );
  52. this.excludedMethods.Add( "get_Profile" );
  53. this.excludedMethods.Add( "get_SupportAutoEvents" );
  54. this.excludedMethods.Add( "get_ApplicationInstance" );
  55. this.excludedMethods.Add( "FrameworkInitialize" );
  56. } // InitializeExcludedMethods
  57. // ----------------------------------------------------------------------
  58. public void InitializeExcludedFields()
  59. {
  60. this.excludedFields.Clear();
  61. this.excludedFields.Add( "__initialized" );
  62. this.excludedFields.Add( "__stringResource" );
  63. } // InitializeExcludedFields
  64. // ----------------------------------------------------------------------
  65. protected virtual void ValidateMethods( UserControl userControl )
  66. {
  67. List<MethodInfo> localMethods = GetLocalMethods( userControl );
  68. if ( localMethods.Count > 0 )
  69. {
  70. StringBuilder sb = new StringBuilder();
  71. foreach ( MethodInfo localMethod in localMethods )
  72. {
  73. if ( sb.Length > 0 )
  74. {
  75. sb.Append( ", " );
  76. }
  77. sb.Append( localMethod.Name );
  78. }
  79. throw new FormSecurityException( "form contains local methods", sb.ToString() );
  80. }
  81. } // ValidateMethods
  82. // ----------------------------------------------------------------------
  83. protected virtual void ValidateFields( UserControl userControl )
  84. {
  85. List<FieldInfo> localFields = GetLocalFields( userControl );
  86. if ( localFields.Count > 0 )
  87. {
  88. StringBuilder sb = new StringBuilder();
  89. foreach ( FieldInfo localField in localFields )
  90. {
  91. if ( sb.Length > 0 )
  92. {
  93. sb.Append( ", " );
  94. }
  95. sb.Append( localField.Name );
  96. }
  97. throw new FormSecurityException( "form contains local fields", sb.ToString() );
  98. }
  99. } // ValidateFields
  100. // ----------------------------------------------------------------------
  101. private List<MethodInfo> GetLocalMethods( UserControl userControl )
  102. {
  103. MethodInfo[] methodInfos = userControl.GetType().GetMethods(
  104. BindingFlags.Public |
  105. BindingFlags.NonPublic |
  106. BindingFlags.Instance |
  107. BindingFlags.Static |
  108. BindingFlags.DeclaredOnly
  109. );
  110. List<MethodInfo> localMethods = new List<MethodInfo>();
  111. foreach ( MethodInfo methodInfo in methodInfos )
  112. {
  113. if ( IsLocalMethod( methodInfo ) )
  114. {
  115. localMethods.Add( methodInfo );
  116. }
  117. }
  118. return localMethods;
  119. } // GetLocalMethods
  120. // ----------------------------------------------------------------------
  121. private List<FieldInfo> GetLocalFields( UserControl userControl )
  122. {
  123. FieldInfo[] fieldInfos = userControl.GetType().GetFields(
  124. BindingFlags.Public |
  125. BindingFlags.NonPublic |
  126. BindingFlags.Instance |
  127. BindingFlags.Static |
  128. BindingFlags.DeclaredOnly
  129. );
  130. List<FieldInfo> localFields = new List<FieldInfo>();
  131. foreach ( FieldInfo fieldInfo in fieldInfos )
  132. {
  133. if ( IsLocalField( fieldInfo ) )
  134. {
  135. localFields.Add( fieldInfo );
  136. }
  137. }
  138. return localFields;
  139. } // GetLocalFields
  140. // ----------------------------------------------------------------------
  141. protected virtual bool IsLocalMethod( MethodInfo methodInfo )
  142. {
  143. // exclude all known methods
  144. foreach ( string excludedMethod in this.excludedMethods )
  145. {
  146. if ( IsExcludedName( excludedMethod, methodInfo.Name ) )
  147. {
  148. return false;
  149. }
  150. }
  151. return true;
  152. } // IsLocalMethod
  153. // ----------------------------------------------------------------------
  154. protected virtual bool IsLocalField( FieldInfo fieldInfo )
  155. {
  156. // exclude all control fields
  157. if ( this.excludeControlFields )
  158. {
  159. Type fieldType = fieldInfo.FieldType;
  160. if ( fieldType.IsSubclassOf( typeof( System.Web.UI.Control ) ) )
  161. {
  162. return false;
  163. }
  164. }
  165. // exclude all known fields
  166. foreach ( string excludedField in this.excludedFields )
  167. {
  168. if ( IsExcludedName( excludedField, fieldInfo.Name ) )
  169. {
  170. return false;
  171. }
  172. }
  173. return true;
  174. } // IsLocalField
  175. // ----------------------------------------------------------------------
  176. protected bool IsExcludedName( string excluded, string name )
  177. {
  178. if ( string.IsNullOrEmpty( excluded ) )
  179. {
  180. return false;
  181. }
  182. if ( excluded.StartsWith( "*" ) )
  183. {
  184. if ( name.EndsWith( excluded.Substring( 1 ) ) )
  185. {
  186. return true;
  187. }
  188. }
  189. if ( excluded.EndsWith( "*" ) )
  190. {
  191. if ( name.StartsWith( excluded.Substring( 0, excluded.Length - 1 ) ) )
  192. {
  193. return true;
  194. }
  195. }
  196. return name.Equals( excluded );
  197. } // IsExcludedName
  198. // ----------------------------------------------------------------------
  199. // members
  200. private readonly List<string> excludedMethods = new List<string>();
  201. private readonly List<string> excludedFields = new List<string>();
  202. private bool excludeControlFields = true;
  203. } // class UserFormCodeValidator
  204. } // namespace Itenso.WebUserForms.Controls
  205. // -- EOF -------------------------------------------------------------------