using System;
using System.Globalization;
using System.Linq;
using System.ComponentModel;
namespace Abp.Extensions
{
///
/// Extension methods for all objects.
///
public static class ObjectExtensions
{
///
/// Used to simplify and beautify casting an object to a type.
///
/// Type to be casted
/// Object to cast
/// Casted object
public static T As(this object obj)
where T : class
{
return (T)obj;
}
///
/// Converts given object to a value type using method.
///
/// Object to be converted
/// Type of the target object
/// Converted object
public static T To(this object obj)
where T : struct
{
if (typeof(T) == typeof(Guid))
{
return (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFromInvariantString(obj.ToString());
}
return (T)Convert.ChangeType(obj, typeof(T), CultureInfo.InvariantCulture);
}
///
/// Check if an item is in a list.
///
/// Item to check
/// List of items
/// Type of the items
public static bool IsIn(this T item, params T[] list)
{
return list.Contains(item);
}
}
}