123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- 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<T>(this ControlCollection coll, T ctl) where T : Control
- {
- coll.Add(ctl);
- return ctl;
- }
- public static T AddEx<T>(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<DescriptionAttribute>()
- .Select(p => p.Description)
- .FirstOrDefault() ?? defaultResult;
- }
- public static bool HasAttribute<T>(this ICustomAttributeProvider cap, bool inherit = true) where T : Attribute
- {
- return cap.IsDefined(typeof(T), inherit);
- }
- }
- internal static class TypeParser<T> 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<T1, T2>(this IEnumerable<T1> source, Func<T1, T2> selector)
- {
- return source.Select(selector).ToArray();
- }
- public static T2[] SelectArray<T1, T2>(this IQueryable<T1> source, Expression<Func<T1, T2>> selector)
- {
- return source.Select(selector).ToArray();
- }
- public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> proc)
- {
- foreach (var item in source)
- proc(item);
- return source;
- }
- public static IEnumerable<T> ForEach<T>(this IQueryable<T> source, Action<T> proc)
- {
- foreach (var item in source)
- proc(item);
- return source;
- }
- public static T2 First<T1, T2>(this Dictionary<T1, T2> 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<string> 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);
- }
- }
|