Example #1
0
        /// <summary>
        /// Parses the encapsulated instance of the specified <paramref name="wrapper"/> for a human-readable string value.
        /// </summary>
        /// <typeparam name="T">The type of the encapsulated instance of <paramref name="wrapper"/>.</typeparam>
        /// <param name="wrapper">The wrapper object to parse the instance.</param>
        /// <returns>A human-readable <see cref="string"/> representation of the wrapped instance in the <see cref="Wrapper{T}"/> object.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="wrapper"/> is null.
        /// </exception>
        public static string ParseInstance <T>(IWrapper <T> wrapper)
        {
            if (wrapper == null)
            {
                throw new ArgumentNullException(nameof(wrapper));
            }
            switch (TypeCodeConverter.FromType(wrapper.InstanceType))
            {
            case TypeCode.Boolean:
                return(wrapper.Instance.ToString().ToLowerInvariant());

            case TypeCode.Byte:
            case TypeCode.Decimal:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
            case TypeCode.SByte:
            case TypeCode.Single:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
            case TypeCode.Double:
                return(wrapper.InstanceAs <IConvertible>().ToString(CultureInfo.InvariantCulture));

            case TypeCode.DateTime:
                return(wrapper.InstanceAs <DateTime>().ToString("O", CultureInfo.InvariantCulture));

            case TypeCode.String:
                return(wrapper.Instance.ToString());

            default:
                if (TypeUtility.IsKeyValuePair(wrapper.InstanceType))
                {
                    PropertyInfo keyProperty   = wrapper.InstanceType.GetProperty("Key");
                    PropertyInfo valueProperty = wrapper.InstanceType.GetProperty("Value");
                    object       keyValue      = keyProperty.GetValue(wrapper.Instance, null) ?? "null";
                    object       valueValue    = valueProperty.GetValue(wrapper.Instance, null) ?? "null";
                    return(string.Format(CultureInfo.InvariantCulture, "[{0},{1}]", keyValue, valueValue));
                }

                if (TypeUtility.IsComparer(wrapper.InstanceType) ||
                    TypeUtility.IsEqualityComparer(wrapper.InstanceType))
                {
                    return(StringConverter.FromType(wrapper.InstanceType));
                }

                switch (wrapper.InstanceType.Name.ToUpperInvariant())
                {
                case "BYTE[]":
                    return(Convert.ToBase64String(wrapper.InstanceAs <byte[]>()));

                case "GUID":
                    return(wrapper.InstanceAs <Guid>(CultureInfo.InvariantCulture).ToString("D"));

                case "RUNTIMETYPE":
                    return(StringConverter.FromType(wrapper.InstanceAs <Type>()));

                case "URI":
                    return(wrapper.InstanceAs <Uri>().OriginalString);

                default:
                    return(wrapper.Instance.ToString());
                }
            }
        }
Example #2
0
 /// <summary>
 /// Determines whether the specified <paramref name="source"/> implements either <see cref="IEqualityComparer"/> or <see cref="IEqualityComparer{T}"/>.
 /// </summary>
 /// <param name="source">The source type to check for implements of either <see cref="IEqualityComparer"/> or <see cref="IEqualityComparer{T}"/>.</param>
 /// <returns><c>true</c> if the specified <paramref name="source"/> implements either <see cref="IEqualityComparer"/> or <see cref="IEqualityComparer{T}"/>; otherwise, <c>false</c>.</returns>
 public static bool IsEqualityComparer(this Type source)
 {
     return(TypeUtility.IsEqualityComparer(source));
 }