UsingDynamicExtensions.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Reflection;
  3. namespace ShwasherSys.ReflectionMagic
  4. {
  5. public static class UsingDynamicExtensions
  6. {
  7. /// <summary>
  8. /// Wraps the specified object in a dynamic object that allows access to private members.
  9. /// </summary>
  10. /// <param name="o">The object to wrap</param>
  11. /// <returns>The wrapped object.</returns>
  12. /// <remarks>
  13. /// Does not wrap <c>null</c>, <see cref="string"/>, primitive types, and already wrapped objects.
  14. /// </remarks>
  15. /// <seealso cref="DynamicObjectInstance"/>
  16. public static dynamic AsDynamic(this object o)
  17. {
  18. // Don't wrap primitive types, which don't have many interesting internal APIs
  19. if (o == null || o.GetType().GetTypeInfo().IsPrimitive || o is string || o is DynamicObjectBase)
  20. return o;
  21. return new DynamicObjectInstance(o);
  22. }
  23. /// <summary>
  24. /// Wraps the specified type in a dynamic object which allows easy instantion through the <see cref="DynamicObjectStatic.New"/> method.
  25. /// </summary>
  26. /// <param name="type">The type to wrap.</param>
  27. /// <returns>The wrapped type.</returns>
  28. /// <seealso cref="DynamicObjectStatic"/>
  29. public static dynamic AsDynamicType(this Type type)
  30. {
  31. return new DynamicObjectStatic(type);
  32. }
  33. /// <summary>
  34. /// Gets the type with the specified name from the specified assembly instance, and returns it as a dynamic object. See also <see cref="AsDynamicType"/>.
  35. /// </summary>
  36. /// <param name="assembly">The assembly instance to search for the type.</param>
  37. /// <param name="typeName">The type name.</param>
  38. /// <returns>The wrapped type.</returns>
  39. /// <seealso cref="AsDynamicType"/>
  40. public static dynamic GetDynamicType(this Assembly assembly, string typeName)
  41. {
  42. if (assembly == null)
  43. throw new ArgumentNullException(nameof(assembly));
  44. return assembly.GetType(typeName).AsDynamicType();
  45. }
  46. /// <summary>
  47. /// Tries to instantiate the type with the specified type name from the specified assembly instance using the specified constructor arguments.
  48. /// </summary>
  49. /// <param name="assembly">The assembly instance to search.</param>
  50. /// <param name="typeName">The full type name.</param>
  51. /// <param name="args">The arguments to pass to the constructor.</param>
  52. /// <returns></returns>
  53. /// <exception cref="MissingMethodException">Thrown when no suitable constructor can be found.</exception>
  54. public static dynamic CreateDynamicInstance(this Assembly assembly, string typeName, params object[] args)
  55. {
  56. if (args == null)
  57. throw new ArgumentNullException(nameof(args));
  58. return assembly.GetDynamicType(typeName).New(args);
  59. }
  60. }
  61. }