Thursday, November 2, 2006

Check parameters of methods the Microsoft way

Microsoft makes heavy use of some common internal methods in .Net 2.0 that check parameters to methods. They are internal, so they can't be used directly. However, using Reflector, we can implement our own version of it. The class is System.Web.Util.SecUtility and is static internal. Below is the source generated by Reflector for the class. namespace System.Web.Util { using System; using System.Collections; using System.Collections.Specialized; using System.Configuration.Provider; using System.Data; using System.Data.SqlClient; using System.Diagnostics; using System.Globalization; using System.Web; using System.Web.Hosting; internal static class SecUtility { internal static void CheckArrayParameter(ref string[] param, bool checkForNull, bool checkIfEmpty, bool checkForCommas, int maxSize, string paramName) { if (param == null) { throw new ArgumentNullException(paramName); } if (param.Length < 1) { throw new ArgumentException(SR.GetString("Parameter_array_empty", new object[] { paramName }), paramName); } Hashtable hashtable1 = new Hashtable(param.Length); for (int num1 = param.Length - 1; num1 >= 0; num1--) { SecUtility.CheckParameter(ref param[num1], checkForNull, checkIfEmpty, checkForCommas, maxSize, paramName + "[ " + num1.ToString(CultureInfo.InvariantCulture) + " ]"); if (hashtable1.Contains(param[num1])) { throw new ArgumentException(SR.GetString("Parameter_duplicate_array_element", new object[] { paramName }), paramName); } hashtable1.Add(param[num1], param[num1]); } } internal static void CheckParameter(ref string param, bool checkForNull, bool checkIfEmpty, bool checkForCommas, int maxSize, string paramName) { if (param == null) { if (checkForNull) { throw new ArgumentNullException(paramName); } } else { param = param.Trim(); if (checkIfEmpty && (param.Length < 1)) { throw new ArgumentException(SR.GetString("Parameter_can_not_be_empty", new object[] { paramName }), paramName); } if ((maxSize > 0) && (param.Length > maxSize)) { throw new ArgumentException(SR.GetString("Parameter_too_long", new object[] { paramName, maxSize.ToString(CultureInfo.InvariantCulture) }), paramName); } if (checkForCommas && param.Contains(",")) { throw new ArgumentException(SR.GetString("Parameter_can_not_contain_comma", new object[] { paramName }), paramName); } } } internal static void CheckPasswordParameter(ref string param, int maxSize, string paramName) { if (param == null) { throw new ArgumentNullException(paramName); } if (param.Length < 1) { throw new ArgumentException(SR.GetString("Parameter_can_not_be_empty", new object[] { paramName }), paramName); } if ((maxSize > 0) && (param.Length > maxSize)) { throw new ArgumentException(SR.GetString("Parameter_too_long", new object[] { paramName, maxSize.ToString(CultureInfo.InvariantCulture) }), paramName); } } internal static void CheckSchemaVersion(ProviderBase provider, SqlConnection connection, string[] features, string version, ref int schemaVersionCheck) { if (connection == null) { throw new ArgumentNullException("connection"); } if (features == null) { throw new ArgumentNullException("features"); } if (version == null) { throw new ArgumentNullException("version"); } if (schemaVersionCheck == -1) { throw new ProviderException(SR.GetString("Provider_Schema_Version_Not_Match", new object[] { provider.ToString(), version })); } if (schemaVersionCheck == 0) { lock (provider) { if (schemaVersionCheck == -1) { throw new ProviderException(SR.GetString("Provider_Schema_Version_Not_Match", new object[] { provider.ToString(), version })); } if (schemaVersionCheck == 0) { SqlCommand command1 = null; SqlParameter parameter1 = null; foreach (string text1 in features) { command1 = new SqlCommand("dbo.aspnet_CheckSchemaVersion", connection); command1.CommandType = CommandType.StoredProcedure; parameter1 = new SqlParameter("@Feature", text1); command1.Parameters.Add(parameter1); parameter1 = new SqlParameter("@CompatibleSchemaVersion", version); command1.Parameters.Add(parameter1); parameter1 = new SqlParameter("@ReturnValue", SqlDbType.Int); parameter1.Direction = ParameterDirection.ReturnValue; command1.Parameters.Add(parameter1); command1.ExecuteNonQuery(); if (((parameter1.Value != null) ? ((int) parameter1.Value) : -1) != 0) { schemaVersionCheck = -1; throw new ProviderException(SR.GetString("Provider_Schema_Version_Not_Match", new object[] { provider.ToString(), version })); } } schemaVersionCheck = 1; } } } } internal static bool GetBooleanValue(NameValueCollection config, string valueName, bool defaultValue) { bool flag1; string text1 = config[valueName]; if (text1 == null) { return defaultValue; } if (!bool.TryParse(text1, out flag1)) { throw new ProviderException(SR.GetString("Value_must_be_boolean", new object[] { valueName })); } return flag1; } internal static string GetDefaultAppName() { try { string text1 = HostingEnvironment.ApplicationVirtualPath; if (string.IsNullOrEmpty(text1)) { text1 = Process.GetCurrentProcess().MainModule.ModuleName; int num1 = text1.IndexOf('.'); if (num1 != -1) { text1 = text1.Remove(num1); } } if (string.IsNullOrEmpty(text1)) { return "/"; } return text1; } catch { return "/"; } } internal static int GetIntValue(NameValueCollection config, string valueName, int defaultValue, bool zeroAllowed, int maxValueAllowed) { int num1; string text1 = config[valueName]; if (text1 == null) { return defaultValue; } if (!int.TryParse(text1, out num1)) { if (zeroAllowed) { throw new ProviderException(SR.GetString("Value_must_be_non_negative_integer", new object[] { valueName })); } throw new ProviderException(SR.GetString("Value_must_be_positive_integer", new object[] { valueName })); } if (zeroAllowed && (num1 < 0)) { throw new ProviderException(SR.GetString("Value_must_be_non_negative_integer", new object[] { valueName })); } if (!zeroAllowed && (num1 <= 0)) { throw new ProviderException(SR.GetString("Value_must_be_positive_integer", new object[] { valueName })); } if ((maxValueAllowed > 0) && (num1 > maxValueAllowed)) { throw new ProviderException(SR.GetString("Value_too_big", new object[] { valueName, maxValueAllowed.ToString(CultureInfo.InvariantCulture) })); } return num1; } internal static bool ValidateParameter(ref string param, bool checkForNull, bool checkIfEmpty, bool checkForCommas, int maxSize) { if (param == null) { return !checkForNull; } param = param.Trim(); if (((!checkIfEmpty || (param.Length >= 1)) && ((maxSize <= 0) || (param.Length <= maxSize))) && (!checkForCommas || !param.Contains(","))) { return true; } return false; } internal static bool ValidatePasswordParameter(ref string param, int maxSize) { if (param == null) { return false; } if (param.Length < 1) { return false; } if ((maxSize > 0) && (param.Length > maxSize)) { return false; } return true; } } }

No comments: