예제 #1
0
 public NumberParser(Type fieldType)
 {
     if (fieldType == typeof(int))
     {
         parseFunction  = (string input, out object value) => { int parsedVal; bool result = int.TryParse(input, out parsedVal); value = parsedVal; return(result); };
         equalsFunction = (object value1, object value2) => (int)value1 == (int)value2;
     }
     else if (fieldType == typeof(uint))
     {
         parseFunction  = (string input, out object value) => { uint parsedVal; bool result = uint.TryParse(input, out parsedVal); value = parsedVal; return(result); };
         equalsFunction = (object value1, object value2) => (uint)value1 == (uint)value2;
     }
     else if (fieldType == typeof(long))
     {
         parseFunction  = (string input, out object value) => { long parsedVal; bool result = long.TryParse(input, out parsedVal); value = parsedVal; return(result); };
         equalsFunction = (object value1, object value2) => (long)value1 == (long)value2;
     }
     else if (fieldType == typeof(ulong))
     {
         parseFunction  = (string input, out object value) => { ulong parsedVal; bool result = ulong.TryParse(input, out parsedVal); value = parsedVal; return(result); };
         equalsFunction = (object value1, object value2) => (ulong)value1 == (ulong)value2;
     }
     else if (fieldType == typeof(byte))
     {
         parseFunction  = (string input, out object value) => { byte parsedVal; bool result = byte.TryParse(input, out parsedVal); value = parsedVal; return(result); };
         equalsFunction = (object value1, object value2) => (byte)value1 == (byte)value2;
     }
     else if (fieldType == typeof(sbyte))
     {
         parseFunction  = (string input, out object value) => { sbyte parsedVal; bool result = sbyte.TryParse(input, out parsedVal); value = parsedVal; return(result); };
         equalsFunction = (object value1, object value2) => (sbyte)value1 == (sbyte)value2;
     }
     else if (fieldType == typeof(short))
     {
         parseFunction  = (string input, out object value) => { short parsedVal; bool result = short.TryParse(input, out parsedVal); value = parsedVal; return(result); };
         equalsFunction = (object value1, object value2) => (short)value1 == (short)value2;
     }
     else if (fieldType == typeof(ushort))
     {
         parseFunction  = (string input, out object value) => { ushort parsedVal; bool result = ushort.TryParse(input, out parsedVal); value = parsedVal; return(result); };
         equalsFunction = (object value1, object value2) => (ushort)value1 == (ushort)value2;
     }
     else if (fieldType == typeof(char))
     {
         parseFunction  = (string input, out object value) => { char parsedVal; bool result = char.TryParse(input, out parsedVal); value = parsedVal; return(result); };
         equalsFunction = (object value1, object value2) => (char)value1 == (char)value2;
     }
     else
     {
         parseFunction  = null;
         equalsFunction = null;
     }
 }
예제 #2
0
 static Inner()
 {
     if (typeof(IEquatable <T>).IsAssignableFrom(typeof(T)))
     {
         var _equals = typeof(IEquatable <T>).GetMethod(nameof(IEquatable <T> .Equals), new[] { typeof(T) });
         if (_equals != null)
         {
             var x = Expression.Parameter(typeof(T).MakeByRefType());
             var y = Expression.Parameter(typeof(T).MakeByRefType());
             equals = Expression.Lambda <EqualsFunc>(Expression.Call(x, _equals, y), x, y).Compile();
         }
     }
     if (equals == null)
     {
         equals = (ref T x, ref T y) => x.Equals(y);
     }
 }
 public NumberParser(Type fieldType)
 {
     if (fieldType == typeof(float))
     {
         parseFunction  = (string input, out object value) => { float parsedVal; bool result = float.TryParse(input, out parsedVal); value = parsedVal; return(result); };
         equalsFunction = (object value1, object value2) => (float)value1 == (float)value2;
     }
     else if (fieldType == typeof(double))
     {
         parseFunction  = (string input, out object value) => { double parsedVal; bool result = double.TryParse(input, out parsedVal); value = parsedVal; return(result); };
         equalsFunction = (object value1, object value2) => (double)value1 == (double)value2;
     }
     else if (fieldType == typeof(decimal))
     {
         parseFunction  = (string input, out object value) => { decimal parsedVal; bool result = decimal.TryParse(input, out parsedVal); value = parsedVal; return(result); };
         equalsFunction = (object value1, object value2) => (decimal)value1 == (decimal)value2;
     }
     else
     {
         parseFunction  = null;
         equalsFunction = null;
     }
 }
예제 #4
0
 /// <summary>
 ///     定义比较函数
 /// </summary>
 /// <param name="equalFunc">Equals的实现函数</param>
 /// <param name="gethashCode">GetHashCode的实现函数</param>
 public CustomEqualityComparer(EqualsFunc <T> equalFunc, GetHashCodeDelgate <T> gethashCode)
 {
     _equalFunc   = equalFunc;
     _gethashCode = gethashCode;
 }
예제 #5
0
 /// <summary>
 ///     定义比较函数
 /// </summary>
 /// <param name="equalFunc">Equals的实现函数</param>
 public CustomEqualityComparer(EqualsFunc <T> equalFunc)
     : this(equalFunc, null)
 {
 }
예제 #6
0
    public NumberParser(Type fieldType)
    {
        // 整型
        if (fieldType == typeof(int))
        {
            parseFunction  = (string input, out object value) => { bool result = int.TryParse(input, out int parsedVal); value = parsedVal; return(result); };
            equalsFunction = (object value1, object value2) => (int)value1 == (int)value2;
        }
        else if (fieldType == typeof(uint))
        {
            parseFunction  = (string input, out object value) => { bool result = uint.TryParse(input, out uint parsedVal); value = parsedVal; return(result); };
            equalsFunction = (object value1, object value2) => (uint)value1 == (uint)value2;
        }
        else if (fieldType == typeof(long))
        {
            parseFunction  = (string input, out object value) => { bool result = long.TryParse(input, out long parsedVal); value = parsedVal; return(result); };
            equalsFunction = (object value1, object value2) => (long)value1 == (long)value2;
        }
        else if (fieldType == typeof(ulong))
        {
            parseFunction  = (string input, out object value) => { bool result = ulong.TryParse(input, out ulong parsedVal); value = parsedVal; return(result); };
            equalsFunction = (object value1, object value2) => (ulong)value1 == (ulong)value2;
        }
        else if (fieldType == typeof(byte))
        {
            parseFunction  = (string input, out object value) => { bool result = byte.TryParse(input, out byte parsedVal); value = parsedVal; return(result); };
            equalsFunction = (object value1, object value2) => (byte)value1 == (byte)value2;
        }
        else if (fieldType == typeof(sbyte))
        {
            parseFunction  = (string input, out object value) => { bool result = sbyte.TryParse(input, out sbyte parsedVal); value = parsedVal; return(result); };
            equalsFunction = (object value1, object value2) => (sbyte)value1 == (sbyte)value2;
        }
        else if (fieldType == typeof(short))
        {
            parseFunction  = (string input, out object value) => { bool result = short.TryParse(input, out short parsedVal); value = parsedVal; return(result); };
            equalsFunction = (object value1, object value2) => (short)value1 == (short)value2;
        }
        else if (fieldType == typeof(ushort))
        {
            parseFunction  = (string input, out object value) => { bool result = ushort.TryParse(input, out ushort parsedVal); value = parsedVal; return(result); };
            equalsFunction = (object value1, object value2) => (ushort)value1 == (ushort)value2;
        }
        else if (fieldType == typeof(char))
        {
            parseFunction  = (string input, out object value) => { bool result = char.TryParse(input, out char parsedVal); value = parsedVal; return(result); };
            equalsFunction = (object value1, object value2) => (char)value1 == (char)value2;
        }

        // 浮点型
        else if (fieldType == typeof(float))
        {
            parseFunction  = (string input, out object value) => { bool result = float.TryParse(input, out float parsedVal); value = parsedVal; return(result); };
            equalsFunction = (object value1, object value2) => (float)value1 == (float)value2;
        }
        else if (fieldType == typeof(double))
        {
            parseFunction  = (string input, out object value) => { bool result = double.TryParse(input, out double parsedVal); value = parsedVal; return(result); };
            equalsFunction = (object value1, object value2) => (double)value1 == (double)value2;
        }
        else if (fieldType == typeof(decimal))
        {
            parseFunction  = (string input, out object value) => { bool result = decimal.TryParse(input, out decimal parsedVal); value = parsedVal; return(result); };
            equalsFunction = (object value1, object value2) => (decimal)value1 == (decimal)value2;
        }
        else
        {
            throw new Exception("Type必须是数字类型");
        }
    }