using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
namespace JiShe.CollectBus.Common.Extensions
{
public static class ObjectExtensions
{
/// Gets the description.
/// The custom attribute provider.
///
///
///
[Description("获取Description")]
public static string GetDescription(this ICustomAttributeProvider customAttributeProvider)
{
var des = string.Empty;
var desAttribute = customAttributeProvider.GetAttribute();
if (desAttribute != null) des = desAttribute.Description;
return des;
}
/// Determines whether this instance is required.
/// The property information.
///
/// true if the specified property information is required; otherwise, false.
[Description("是否必填")]
public static bool IsRequired(this PropertyInfo propertyInfo)
{
if (propertyInfo.GetAttribute(true) != null) return true;
//Boolean、Byte、SByte、Int16、UInt16、Int32、UInt32、Int64、UInt64、Char、Double、Single
if (propertyInfo.PropertyType.IsPrimitive) return true;
switch (propertyInfo.PropertyType.Name)
{
case "DateTime":
case "Decimal":
return true;
}
return false;
}
/// Gets the attribute.
///
/// The assembly.
/// if set to true [inherit].
///
///
///
[Description("获取属性")]
public static T GetAttribute(this ICustomAttributeProvider assembly, bool inherit = false)
where T : Attribute
{
return assembly
.GetCustomAttributes(typeof(T), inherit)
.OfType()
.FirstOrDefault();
}
///
/// Enums to list.
///
///
///
/// T must be of type System.Enum
[Description("枚举转换为集合")]
public static List EnumToList()
{
var enumType = typeof(T);
if (enumType.BaseType != typeof(Enum))
throw new ArgumentException("T must be of type System.Enum");
var enumValArray = Enum.GetValues(enumType);
var enumValList = new List(enumValArray.Length);
enumValList.AddRange(from int val in enumValArray select (T)Enum.Parse(enumType, val.ToString()));
return enumValList;
}
///
/// Enums to dictionary.
///
/// The t.
///
///
/// object is not an Enumeration
[Description("枚举转换为字典")]
public static IDictionary EnumToDictionary(this Type t)
{
if (t == null) throw new NullReferenceException();
if (!t.IsEnum) throw new InvalidCastException("object is not an Enumeration");
var names = Enum.GetNames(t);
var values = Enum.GetValues(t);
return (from i in Enumerable.Range(0, names.Length)
select new { Key = names[i], Value = (int)values.GetValue(i) }).ToDictionary(k => k.Key, k => k.Value);
}
///
/// 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 or enum type using or 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) || typeof(T) == typeof(TimeSpan))
{
return (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFromInvariantString(obj.ToString());
}
if (typeof(T).IsEnum)
{
if (Enum.IsDefined(typeof(T), obj))
{
return (T)Enum.Parse(typeof(T), obj.ToString());
}
else
{
throw new ArgumentException($"Enum type undefined '{obj}'.");
}
}
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);
}
}
}