ExtensionMethods.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. namespace Caliburn.Micro.Core {
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. /// <summary>
  7. /// Generic extension methods used by the framework.
  8. /// </summary>
  9. public static class ExtensionMethods {
  10. /// <summary>
  11. /// Get's the name of the assembly.
  12. /// </summary>
  13. /// <param name="assembly">The assembly.</param>
  14. /// <returns>The assembly's name.</returns>
  15. public static string GetAssemblyName(this Assembly assembly) {
  16. return assembly.FullName.Remove(assembly.FullName.IndexOf(','));
  17. }
  18. /// <summary>
  19. /// Gets all the attributes of a particular type.
  20. /// </summary>
  21. /// <typeparam name="T">The type of attributes to get.</typeparam>
  22. /// <param name="member">The member to inspect for attributes.</param>
  23. /// <param name="inherit">Whether or not to search for inherited attributes.</param>
  24. /// <returns>The list of attributes found.</returns>
  25. public static IEnumerable<T> GetAttributes<T>(this MemberInfo member, bool inherit) {
  26. #if WinRT || CORE
  27. return member.GetCustomAttributes(inherit).OfType<T>();
  28. #else
  29. return Attribute.GetCustomAttributes(member, inherit).OfType<T>();
  30. #endif
  31. }
  32. #if WinRT || CORE
  33. /// <summary>
  34. /// Gets a collection of the public types defined in this assembly that are visible outside the assembly.
  35. /// </summary>
  36. /// <param name="assembly">The assembly.</param>
  37. /// <returns>A collection of the public types defined in this assembly that are visible outside the assembly.</returns>
  38. /// <exception cref="ArgumentNullException"></exception>
  39. public static IEnumerable<Type> GetExportedTypes(this Assembly assembly) {
  40. if (assembly == null)
  41. throw new ArgumentNullException("assembly");
  42. return assembly.ExportedTypes;
  43. }
  44. /// <summary>
  45. /// Returns a value that indicates whether the specified type can be assigned to the current type.
  46. /// </summary>
  47. /// <param name="target">The target type</param>
  48. /// <param name="type">The type to check.</param>
  49. /// <returns>true if the specified type can be assigned to this type; otherwise, false.</returns>
  50. public static bool IsAssignableFrom(this Type target, Type type) {
  51. return target.GetTypeInfo().IsAssignableFrom(type.GetTypeInfo());
  52. }
  53. #endif
  54. /// <summary>
  55. /// Gets the value for a key. If the key does not exist, return default(TValue);
  56. /// </summary>
  57. /// <typeparam name="TKey">The type of the keys in the dictionary.</typeparam>
  58. /// <typeparam name="TValue">The type of the values in the dictionary.</typeparam>
  59. /// <param name="dictionary">The dictionary to call this method on.</param>
  60. /// <param name="key">The key to look up.</param>
  61. /// <returns>The key value. default(TValue) if this key is not in the dictionary.</returns>
  62. public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key) {
  63. TValue result;
  64. return dictionary.TryGetValue(key, out result) ? result : default(TValue);
  65. }
  66. }
  67. }