using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Web.UI; using System.Web.UI.WebControls; internal static class _PageExt { public static T AddEx(this ControlCollection coll, T ctl) where T : Control { coll.Add(ctl); return ctl; } public static T AddEx(this TableCellCollection coll, T ctl) where T : TableCell { coll.Add(ctl); return ctl; } } internal static class _ControlExt { public static void VisualText(this TableCell td, string text) { if (text == null) { td.ForeColor = Color.Red; td.Text = "NULL"; } else if (string.IsNullOrEmpty(text)) { td.ForeColor = Color.Orange; td.Text = "EMPTY"; } else { td.Text = text; } } } internal static class _AttributeExt { public static string GetDescriptionByAttribute(this ICustomAttributeProvider cap, bool inherit = true, string defaultResult = "") { return cap.GetCustomAttributes(inherit) .OfType() .Select(p => p.Description) .FirstOrDefault() ?? defaultResult; } public static bool HasAttribute(this ICustomAttributeProvider cap, bool inherit = true) where T : Attribute { return cap.IsDefined(typeof(T), inherit); } } internal static class TypeParser where T : struct { private delegate bool DTryParse(string value, out T result); private static DTryParse ms_dTryParse; static TypeParser() { Type type = typeof(T); if (type.Name == "Nullable`1") throw new NotSupportedException("TypeParser not support Nullable type"); var mTryParse = type .GetMethods( BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.InvokeMethod ).FirstOrDefault(p => p.ReturnType == typeof(bool) && p.GetParameters().Length == 2 && p.GetParameters()[0].ParameterType == typeof(string) && p.GetParameters()[1].IsOut && p.GetParameters()[1].ParameterType.GetElementType() == type ); if (mTryParse == null) throw new NotSupportedException("TypeParser can no found `bool TypParse(String,out " + type.Name + ")` metohd"); ms_dTryParse = (DTryParse)Delegate.CreateDelegate(typeof(DTryParse), mTryParse); } public static T Convert(object value) { if (value == null || value == DBNull.Value) return default(T); T result = default(T); ms_dTryParse(value.ToString(), out result); return result; } public static T? ConvertNullable(object value) { if (value == null || value == DBNull.Value) return null; T result; if (ms_dTryParse(value.ToString(), out result)) return result; return null; } } internal static class _LinqUtility { public static T2[] SelectArray(this IEnumerable source, Func selector) { return source.Select(selector).ToArray(); } public static T2[] SelectArray(this IQueryable source, Expression> selector) { return source.Select(selector).ToArray(); } public static IEnumerable ForEach(this IEnumerable source, Action proc) { foreach (var item in source) proc(item); return source; } public static IEnumerable ForEach(this IQueryable source, Action proc) { foreach (var item in source) proc(item); return source; } public static T2 First(this Dictionary source) { foreach (var item in source) { return item.Value; } throw new InvalidOperationException("No items in dic"); } } internal static class _StringExt { public static string ToDisplayString(this object value) { if (value is bool) return (bool)value ? "是" : "否"; if (value is Enum) { return value.GetType().GetField(value.ToString()).GetDescriptionByAttribute() ?? value.ToString(); } if (value == null) return null; return value.ToString(); } public static string JoinString(this IEnumerable source, string separator) { return string.Join(separator, source.ToArray()); } public static string ToHexString(this byte[] buf) { return buf.SelectArray(p => p.ToString("X2")) .JoinString(""); } public static string Format(this string fmt, params object[] args) { return string.Format(fmt, args); } } internal static class _DataExt { public static byte[] ToBytes(this int value) { return BitConverter.GetBytes(value); } }