context: ///////////////////////////////////////////////////////////////////////////////////////
  // Copyright (C) 2006-2019 Esper Team. All rights reserved.                           /
  // http://esper.codehaus.org                                                          /
  // ---------------------------------------------------------------------------------- /
  // The software in this package is published under the terms of the GPL license       /
  // a copy of which has been included with this distribution in the license.txt file.  /
  ///////////////////////////////////////////////////////////////////////////////////////
  
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Numerics;
  using System.Reflection;
  using System.Text;
  
  using com.espertech.esper.compat.collections;
  
  namespace com.espertech.esper.compat
  {
      public static class TypeExtensions
      {
          /// <summary>
          /// Finds the property.
          /// </summary>
          /// <returns></returns>
          public static PropertyInfo FindProperty(this Type t, string name)
          {
              var propertyInfo = t.GetProperty(name);
              if (propertyInfo != null)
                  return propertyInfo;
  
              foreach( var property in t.GetProperties() ) {
                  if ( String.Equals(name, property.Name, StringComparison.CurrentCultureIgnoreCase) ) {
                      return property;
                  }
              }
  
              return null;
          }
  
          /// <summary>
          /// Ases the singleton.
          /// </summary>
          /// <typeparam name="T"></typeparam>
          /// <param name="value">The value.</param>
          /// <returns></returns>
          public static T[] AsSingleton<T>(this T value)
          {
              return new T[] {value};
          }
  
          public static ISet<T> AsSet<T>(this T value)
          {
              return new Singleton<T>(value);
          }
  
          public static byte? AsBoxedByte(this object value)
          {
              if (value == null) return null;
              if (value is byte) return (byte) value;
              return Convert.ToByte(value);
          }
  
          public static byte AsByte(this object value)
          {
              if (value is byte)
                  return (byte) value;
              return Convert.ToByte(value);
          }
  
          /// <summary>
          /// Returns the value as a boxed short.
          /// </summary>
          /// <param name="value">The value.</param>
          /// <returns></returns>
          public static short? AsBoxedInt16(this object value)
          {
              if (value == null) return null;
              if (value is short) return (short) value;
              return AsInt16(value);
          }
  
          /// <summary>
          /// Returns the value as a short.
          /// </summary>
          /// <param name="value">The value.</param>
          /// <returns></returns>
          public static short AsInt16(this object value)
          {
              if (value is short shortValue)
                  return shortValue;
              if (value is byte byteValue)
                  return byteValue;
  
              // -- Down-casts / Cross-casts
              if (value is int intValue)
                  return (short) intValue;
              if (value is long longValue)
                  return (short) longValue;
              if (value is float floatValue)
                  return (short) floatValue;
              if (value is double doubleValue)
                  return (short) doubleValue;
              if (value is decimal decimalValue)
                  return (short) decimalValue;
  
              return Convert.ToInt16(value);
          }
  
          /// <summary>
          /// Returns the value as a boxed int.
          /// </summary>
          /// <param name="value">The value.</param>
          /// <returns></returns>
          public static int? AsBoxedInt32(this object value)
          {
              if (value == null) return null;
              if (value is int intValue) return intValue;
              return AsInt32(value);
          }
  
          /// <summary>
          /// Returns the value as an int.
          /// </summary>
          /// <param name="value">The value.</param>
          /// <returns></returns>
          public static int AsInt32(this object value)
          {
              if (value is int intValue)
                  return intValue;
              if (value is short shortValue)
                  return shortValue;
              if (value is byte byteValue)
                  return byteValue;
              
              // -- Down-casts / Cross-casts
              if (value is long longValue)
                  return (int) longValue;
              if (value is float floatValue)
                  return (int) floatValue;
              if (value is double doubleValue)
                  return (int) doubleValue;
              if (value is decimal decimalValue)
                  return (int) decimalValue;
  
              return Convert.ToInt32(value);
          }
  
          /// <summary>
          /// Returns the value as a boxed long.
          /// </summary>
          /// <param name="value">The value.</param>
          /// <returns></returns>
          public static long? AsBoxedInt64(this object value)
          {
              if (value == null) return null;
              if (value is long longValue) return longValue;
              return AsInt64(value);
          }
  
          /// <summary>
          /// Returns the value as a long.
          /// </summary>
          /// <param name="value">The value.</param>
          /// <returns></returns>
          public static long AsInt64(this object value)
          {
              if (value is long longValue)
                  return longValue;
              if (value is int intValue)
                  return intValue;
              if (value is short shortValue)
                  return shortValue;
              if (value is byte byteValue)
                  return byteValue;
  
              // -- Down-casts / Cross-casts
              if (value is float floatValue)
                  return (long) floatValue;
              if (value is double doubleValue)
                  return (long) doubleValue;
              if (value is decimal decimalValue)
                  return (long) decimalValue;
  
              return Convert.ToInt64(value);
          }
          
          public static ulong AsUInt64(this object value)
          {
              if (value is ulong longValue)
                  return longValue;
              if (value is uint intValue)
                  return intValue;
              if (value is ushort shortValue)
                  return shortValue;
              if (value is byte byteValue)
                  return byteValue;
  
              // -- Down-casts / Cross-casts
              if (value is float floatValue)
                  return (ulong) floatValue;
              if (value is double doubleValue)
                  return (ulong) doubleValue;
              if (value is decimal decimalValue)
                  return (ulong) decimalValue;
  
              return Convert.ToUInt64(value);
          }
  
          public static uint AsUInt32(this object value)
          {
              if (value is uint intValue)
                  return intValue;
              if (value is ushort shortValue)
                  return shortValue;
              if (value is byte byteValue)
                  return byteValue;
  
              // -- Down-casts / Cross-casts
              if (value is float floatValue)
                  return (uint) floatValue;
              if (value is double doubleValue)
                  return (uint) doubleValue;
              if (value is decimal decimalValue)
                  return (uint) decimalValue;
  
              return Convert.ToUInt32(value);
          }
  
          public static ushort AsUInt16(this object value)
          {
              if (value is ushort shortValue)
                  return shortValue;
              if (value is byte byteValue)
                  return byteValue;
  
              // -- Down-casts / Cross-casts
              if (value is float floatValue)
                  return (ushort) floatValue;
              if (value is double doubleValue)
                  return (ushort) doubleValue;
              if (value is decimal decimalValue)
                  return (ushort) decimalValue;
  
              return Convert.ToUInt16(value);
          }
          
          /// <summary>
          /// Returns the value as a float.
          /// </summary>
          /// <param name="value">The value.</param>
          /// <returns></returns>
          public static float AsFloat(this object value)
          {
              if (value is float floatValue)
                  return floatValue;
              if (value is double doubleValue)
                  return (float) doubleValue;
              if (value is decimal decimalValue)
                  return (float) decimalValue;
  
              if (value is long longValue)
                  return longValue;
              if (value is int intValue)
                  return intValue;
              if (value is short shortValue)
                  return shortValue;
              if (value is byte byteValue)
                  return byteValue;
              
              if (value is BigInteger bigIntegerValue)
                  return (float) bigIntegerValue;
  
              return Convert.ToSingle(value);
          }
  
          /// <summary>
          /// Returns the value as a boxed float.
          /// </summary>
          /// <param name="value">The value.</param>
          /// <returns></returns>
          public static float? AsBoxedFloat(this object value)
          {
              if (value == null)
                  return null;
              return AsFloat(value);
          }
  
          /// <summary>
          /// Returns the value as a double.
          /// </summary>
          /// <param name="value">The value.</param>
          /// <returns></returns>
          public static double AsDouble(this object value)
          {
              if (value is double doubleValue)
                  return doubleValue;
              if (value is float floatValue)
                  return floatValue;
  
              if (value is long longValue)
                  return longValue;
              if (value is int intValue)
                  return intValue;
              if (value is short shortValue)
                  return shortValue;
              if (value is byte byteValue)
                  return byteValue;
  
              // -- Down-casts / Cross-casts
              if (value is decimal decimalValue)
                  return (double) decimalValue;
              if (value is BigInteger bigIntegerValue)
  
gt:                 return (double) bigIntegerValue;