namespace Caliburn.Micro.Core {
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    /// 
    /// Generic extension methods used by the framework.
    /// 
    public static class ExtensionMethods {
        /// 
        /// Get's the name of the assembly.
        /// 
        /// The assembly.
        /// The assembly's name.
        public static string GetAssemblyName(this Assembly assembly) {
            return assembly.FullName.Remove(assembly.FullName.IndexOf(','));
        }
        /// 
        /// Gets all the attributes of a particular type.
        /// 
        /// The type of attributes to get.
        /// The member to inspect for attributes.
        /// Whether or not to search for inherited attributes.
        /// The list of attributes found.
        public static IEnumerable GetAttributes(this MemberInfo member, bool inherit) {
#if WinRT || CORE
            return member.GetCustomAttributes(inherit).OfType();
#else
            return Attribute.GetCustomAttributes(member, inherit).OfType();
#endif
        }
#if WinRT || CORE
        /// 
        /// Gets a collection of the public types defined in this assembly that are visible outside the assembly.
        /// 
        /// The assembly.
        /// A collection of the public types defined in this assembly that are visible outside the assembly.
        /// 
        public static IEnumerable GetExportedTypes(this Assembly assembly) {
            if (assembly == null)
                throw new ArgumentNullException("assembly");
            return assembly.ExportedTypes;
        }
        /// 
        /// Returns a value that indicates whether the specified type can be assigned to the current type.
        /// 
        /// The target type
        /// The type to check.
        /// true if the specified type can be assigned to this type; otherwise, false.
        public static bool IsAssignableFrom(this Type target, Type type) {
            return target.GetTypeInfo().IsAssignableFrom(type.GetTypeInfo());
        }
#endif
        /// 
        /// Gets the value for a key. If the key does not exist, return default(TValue);
        /// 
        /// The type of the keys in the dictionary.
        /// The type of the values in the dictionary.
        /// The dictionary to call this method on.
        /// The key to look up.
        /// The key value. default(TValue) if this key is not in the dictionary.
        public static TValue GetValueOrDefault(this IDictionary dictionary, TKey key) {
            TValue result;
            return dictionary.TryGetValue(key, out result) ? result : default(TValue);
        }
    }
}