2013-04-22 00:04:09 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
namespace NzbDrone.Common.Reflection
|
|
|
|
{
|
|
|
|
public static class ReflectionExtensions
|
|
|
|
{
|
2013-09-23 02:40:36 +03:00
|
|
|
public static readonly Assembly CoreAssembly = Assembly.Load("NzbDrone.Core");
|
|
|
|
|
2013-04-22 00:04:09 +03:00
|
|
|
public static List<PropertyInfo> GetSimpleProperties(this Type type)
|
|
|
|
{
|
|
|
|
var properties = type.GetProperties();
|
|
|
|
return properties.Where(c => c.PropertyType.IsSimpleType()).ToList();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static List<Type> ImplementationsOf<T>(this Assembly assembly)
|
|
|
|
{
|
|
|
|
return assembly.GetTypes().Where(c => typeof(T).IsAssignableFrom(c)).ToList();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static bool IsSimpleType(this Type type)
|
|
|
|
{
|
2013-04-28 22:46:13 +03:00
|
|
|
if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Nullable<>) || type.GetGenericTypeDefinition() == typeof(List<>)))
|
2013-04-22 00:04:09 +03:00
|
|
|
{
|
|
|
|
type = type.GetGenericArguments()[0];
|
|
|
|
}
|
|
|
|
|
2013-04-28 22:46:13 +03:00
|
|
|
|
2013-04-22 00:04:09 +03:00
|
|
|
return type.IsPrimitive
|
|
|
|
|| type.IsEnum
|
|
|
|
|| type == typeof(string)
|
|
|
|
|| type == typeof(DateTime)
|
2013-05-20 03:30:02 +03:00
|
|
|
|| type == typeof(Version)
|
2013-06-13 04:37:05 +03:00
|
|
|
|| type == typeof(Decimal);
|
2013-04-22 00:04:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public static bool IsReadable(this PropertyInfo propertyInfo)
|
|
|
|
{
|
|
|
|
return propertyInfo.CanRead && propertyInfo.GetGetMethod(false) != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool IsWritable(this PropertyInfo propertyInfo)
|
|
|
|
{
|
|
|
|
return propertyInfo.CanWrite && propertyInfo.GetSetMethod(false) != null;
|
|
|
|
}
|
|
|
|
|
2013-05-01 09:43:14 +03:00
|
|
|
public static T GetAttribute<T>(this MemberInfo member, bool isRequired = true) where T : Attribute
|
|
|
|
{
|
|
|
|
var attribute = member.GetCustomAttributes(typeof(T), false).SingleOrDefault();
|
|
|
|
|
|
|
|
if (attribute == null && isRequired)
|
|
|
|
{
|
|
|
|
throw new ArgumentException(String.Format("The {0} attribute must be defined on member {1}", typeof(T).Name, member.Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
return (T)attribute;
|
|
|
|
}
|
2013-05-09 09:38:20 +03:00
|
|
|
|
2013-09-23 02:40:36 +03:00
|
|
|
public static Type FindTypeByName(this Assembly assembly, string name)
|
|
|
|
{
|
2013-09-25 03:01:03 +03:00
|
|
|
return assembly.GetTypes().SingleOrDefault(c => c.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase));
|
2013-09-23 02:40:36 +03:00
|
|
|
}
|
|
|
|
|
2013-05-09 09:38:20 +03:00
|
|
|
public static bool HasAttribute<TAttribute>(this Type type)
|
|
|
|
{
|
|
|
|
return type.GetCustomAttributes(typeof(TAttribute), true).Any();
|
|
|
|
}
|
2013-04-22 00:04:09 +03:00
|
|
|
}
|
|
|
|
}
|