// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) by respective owners including Yahoo!, Microsoft, and
// individual contributors. All rights reserved. Released under a BSD
// license as described in the file LICENSE.
//
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
namespace VW.Reflection
{
///
/// Utilitiy class supporting feature type inspection.
///
public static class InspectionHelper
{
///
/// Determines if the is a supported numeric type.
///
/// The type to be inspected.
/// True if numeric, false otherwise.
public static bool IsNumericType(Type elemType)
{
return IsNumericTypeInternal(elemType) ||
(elemType != null
&& elemType.IsGenericType
&& elemType.GetGenericTypeDefinition() == typeof(Nullable<>)
&& IsNumericTypeInternal(elemType.GetGenericArguments()[0]));
}
private static bool IsNumericTypeInternal(Type elemType)
{
return elemType == typeof(double)
|| elemType == typeof(float)
|| elemType == typeof(byte)
|| elemType == typeof(sbyte)
|| elemType == typeof(char)
|| elemType == typeof(decimal)
|| elemType == typeof(UInt16)
|| elemType == typeof(UInt32)
|| elemType == typeof(UInt64)
|| elemType == typeof(Int16)
|| elemType == typeof(Int32)
|| elemType == typeof(Int64);
}
///
/// If is an enumerable type (such as array or ), this method will
/// return the element type.
///
/// The type to be inspected.
/// If is an enumerable type the element type is returned, otherwise null.
public static Type GetEnumerableElementType(Type type)
{
Contract.Requires(type != null);
if (type.IsArray)
return type.GetElementType();
var enumerableType = type.GetInterfaces().Union(new[] { type })
.FirstOrDefault(it => it.IsGenericType && it.GetGenericTypeDefinition() == typeof(IEnumerable<>));
// let's get T of IEnumerable
if (enumerableType != null)
return enumerableType.GetGenericArguments()[0];
return null;
}
}
}