using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Reflection; namespace System.Linq.Expressions { static class ExpressionUtil { public static bool IsNumber(Type type) { // enum can also be in a numeric form, but we // want it to be considered as a number? if (type.IsEnum) return false; TypeCode typeCode = Type.GetTypeCode(type); if (typeCode == TypeCode.Byte || typeCode == TypeCode.Decimal || typeCode == TypeCode.Double || typeCode == TypeCode.Int16 || typeCode == TypeCode.Int32 || typeCode == TypeCode.Int64 || typeCode == TypeCode.Single) return true; return false; } public static bool IsNumber(object value) { if (value == null) return false; return IsNumber(value.GetType()); } /// /// tries to find the method with the given name in one of the /// given types in the array. /// /// The name of the method to find. /// An array of (typically 2) defining /// the types we're going to check for the method. /// public static MethodInfo GetMethod(string methodName, Type[] types) { if (types.Length > 2) throw new ArgumentException(); BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static; // the method should have the two types defined as argument... MethodInfo method = types[0].GetMethod(methodName, flags, null, types, null); if (method == null && types.Length > 1) method = types[1].GetMethod(methodName, flags, null, types, null); return method; } public static ReadOnlyCollection GetReadOnlyCollection(IEnumerable en) { if (en == null) return new ReadOnlyCollection(new MemberBinding[0]); List list = new List(en); return new ReadOnlyCollection(list); } public static ReadOnlyCollection GetReadOnlyCollection(IEnumerable en) { if (en == null) return new ReadOnlyCollection(new Expression[0]); List list = new List(en); return new ReadOnlyCollection(list); } } }