Mega Code Archive
Returns equality with type conversion
namespace System.Web.Mvc {
using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.Routing;
using System.Web.UI;
using System.Collections.Generic;
internal static class HtmlExtensionUtility {
///
/// Returns equality with type conversion
///
/// Object to compare to
/// Object being compared
/// System.Boolean
internal static bool AreEqual(object o1, object o2) {
bool result = false;
if (o1 == null && o2 == null) {
result = true;
}
else {
if (o1 != null) {
Type t = o1.GetType();
try {
o2 = Convert.ChangeType(o2, t, CultureInfo.CurrentCulture);
}
catch (FormatException x) {
throw new FormatException("Can't compare \"" + o1.ToString() + "\" (" + o1.GetType().Name + ") to \"" + o2.ToString() + "\" (" + o2.GetType().Name + ") because the types don't match. Are you comparing the right values?", x);
}
result = o1.Equals(o2);
}
}
return result;
}
///
/// Renders a System.Web.Page and returns it's rendered page
///
/// Page to render
/// System.String
internal static string RenderPage(Page sourcePage) {
//render
StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb, CultureInfo.CurrentCulture);
HttpContext.Current.Server.Execute(sourcePage, writer, true);
string result = sb.ToString();
return result;
}
///
/// Sets the properties of an object based on a HashTable
///
///
///
internal static void SetPropertiesFromDictionary(object instance, RouteValueDictionary settings) {
foreach (string key in settings.Keys) {
//find a matching property
PropertyInfo propInfo = instance.GetType().GetProperty(key, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);
if (propInfo != null) {
propInfo.SetValue(instance, settings[key], null);
}
}
}
///
/// Creates a simple {0}="{1}" list based on current object state.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase",
Justification = "We want the value to be lower case since it is used in HTML.")]
public static string ConvertObjectToAttributeList(object value) {
StringBuilder sb = new StringBuilder();
if (value != null) {
IDictionary d = value as IDictionary;
if (d == null) {
d = new RouteValueDictionary(value);
}
string resultFormat = "{0}=\"{1}\" ";
foreach (string attribute in d.Keys) {
object thisValue = d[attribute];
if (d[attribute] is bool) {
thisValue = d[attribute].ToString().ToLowerInvariant();
}
sb.AppendFormat(resultFormat, attribute.Replace("_", "").ToLowerInvariant(), thisValue);
}
}
return sb.ToString();
}
}
}