123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Reflection;
- using System.Reflection.Emit;
- namespace Bowin.Common.Utility
- {
- public static class WebExtensions
- {
- public static T ParseTo<T>(this string value)
- {
- if (Parser<T>.ParseMethod != null)
- return Parser<T>.ParseMethod(value);
- throw new NotSupportedException();
- }
- public static bool TryParseTo<T>(this string value, out T result)
- {
- if (Parser<T>.TryParseMethod != null)
- {
- return Parser<T>.TryParseMethod(value, out result);
- }
- throw new NotSupportedException();
- }
- public static Nullable<T> ParseStrTo<T>(this string value) where T : struct
- {
- if (value == null) return null;
- if (Parser<T>.TryParseMethod != null)
- {
- T result;
- if (Parser<T>.TryParseMethod(value, out result))
- {
- return result;
- }
- else
- {
- return null;
- }
- }
- throw new NotSupportedException();
- }
- static WebExtensions()
- {
- Parser<short>.ParseMethod = short.Parse;
- Parser<int>.ParseMethod = int.Parse;
- Parser<long>.ParseMethod = long.Parse;
- Parser<byte>.ParseMethod = byte.Parse;
- Parser<ushort>.ParseMethod = ushort.Parse;
- Parser<uint>.ParseMethod = uint.Parse;
- Parser<ulong>.ParseMethod = ulong.Parse;
- Parser<sbyte>.ParseMethod = sbyte.Parse;
- Parser<float>.ParseMethod = float.Parse;
- Parser<double>.ParseMethod = double.Parse;
- Parser<decimal>.ParseMethod = decimal.Parse;
- Parser<bool>.ParseMethod = bool.Parse;
- Parser<DateTime>.ParseMethod = DateTime.Parse;
- Parser<TimeSpan>.ParseMethod = TimeSpan.Parse;
- Parser<Guid>.ParseMethod = Guid.Parse;
- Parser<short>.TryParseMethod = short.TryParse;
- Parser<int>.TryParseMethod = int.TryParse;
- Parser<long>.TryParseMethod = long.TryParse;
- Parser<byte>.TryParseMethod = byte.TryParse;
- Parser<ushort>.TryParseMethod = ushort.TryParse;
- Parser<uint>.TryParseMethod = uint.TryParse;
- Parser<ulong>.TryParseMethod = ulong.TryParse;
- Parser<sbyte>.TryParseMethod = sbyte.TryParse;
- Parser<float>.TryParseMethod = float.TryParse;
- Parser<double>.TryParseMethod = double.TryParse;
- Parser<decimal>.TryParseMethod = decimal.TryParse;
- Parser<bool>.TryParseMethod = bool.TryParse;
- Parser<DateTime>.TryParseMethod = DateTime.TryParse;
- Parser<TimeSpan>.TryParseMethod = TimeSpan.TryParse;
- Parser<Guid>.TryParseMethod = Guid.TryParse;
- }
- private class Parser<T>
- {
- public delegate T ParseMethodDelegate(string value);
- public delegate bool TryParseMethodDelegate(string value, out T result);
- private static bool noParseMethod = false;
- private static ParseMethodDelegate _parseMethod;
- public static ParseMethodDelegate ParseMethod
- {
- get
- {
- if (_parseMethod != null)
- return _parseMethod;
- if (noParseMethod)
- return null;
- var method = typeof(T).GetMethod("Parse", new Type[] { typeof(string) });
- if (method != null && (method.Attributes & MethodAttributes.Static) != 0)
- {
- DynamicMethod dynamicMethod = new DynamicMethod(typeof(T).FullName + "_Parse", typeof(T), new Type[] { typeof(string) });
- var il = dynamicMethod.GetILGenerator();
- il.Emit(OpCodes.Ldarg_0);
- il.EmitCall(OpCodes.Call, method, null);
- il.Emit(OpCodes.Ret);
- return _parseMethod = (Parser<T>.ParseMethodDelegate)dynamicMethod.CreateDelegate(typeof(Parser<T>.ParseMethodDelegate));
- }
- noParseMethod = true;
- return null;
- }
- set
- {
- _parseMethod = value;
- }
- }
- private static bool noTryParseMethod = false;
- private static TryParseMethodDelegate _tryParseMethod;
- public static TryParseMethodDelegate TryParseMethod
- {
- get
- {
- if (_tryParseMethod != null)
- return _tryParseMethod;
- if (noTryParseMethod)
- return null;
- MethodInfo tryParseMethod = typeof(T).GetMethod("TryParse", new Type[] { typeof(string), Type.GetType(typeof(T).FullName + "&") });
- if (tryParseMethod != null)
- {
- DynamicMethod tryParseDm = new DynamicMethod("TryParse", typeof(bool),
- new Type[] { typeof(string), Type.GetType(typeof(T).FullName + "&") },
- typeof(Parser<T>), false);
- ILGenerator tryParseIl = tryParseDm.GetILGenerator();
- tryParseIl.Emit(OpCodes.Ldarg_0);
- tryParseIl.Emit(OpCodes.Ldarg_1);
- tryParseIl.Emit(OpCodes.Call, tryParseMethod);
- tryParseIl.Emit(OpCodes.Ret);
- return _tryParseMethod = (TryParseMethodDelegate)tryParseDm.CreateDelegate(typeof(TryParseMethodDelegate));
- }
- noTryParseMethod = true;
- return null;
- }
- set
- {
- _tryParseMethod = value;
- }
- }
- }
- }
- }
|