| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using System;
- using System.Reflection;
- namespace ShwasherSys.ReflectionMagic
- {
- public static class UsingDynamicExtensions
- {
- /// <summary>
- /// Wraps the specified object in a dynamic object that allows access to private members.
- /// </summary>
- /// <param name="o">The object to wrap</param>
- /// <returns>The wrapped object.</returns>
- /// <remarks>
- /// Does not wrap <c>null</c>, <see cref="string"/>, primitive types, and already wrapped objects.
- /// </remarks>
- /// <seealso cref="DynamicObjectInstance"/>
- public static dynamic AsDynamic(this object o)
- {
- // Don't wrap primitive types, which don't have many interesting internal APIs
- if (o == null || o.GetType().GetTypeInfo().IsPrimitive || o is string || o is DynamicObjectBase)
- return o;
- return new DynamicObjectInstance(o);
- }
- /// <summary>
- /// Wraps the specified type in a dynamic object which allows easy instantion through the <see cref="DynamicObjectStatic.New"/> method.
- /// </summary>
- /// <param name="type">The type to wrap.</param>
- /// <returns>The wrapped type.</returns>
- /// <seealso cref="DynamicObjectStatic"/>
- public static dynamic AsDynamicType(this Type type)
- {
- return new DynamicObjectStatic(type);
- }
- /// <summary>
- /// Gets the type with the specified name from the specified assembly instance, and returns it as a dynamic object. See also <see cref="AsDynamicType"/>.
- /// </summary>
- /// <param name="assembly">The assembly instance to search for the type.</param>
- /// <param name="typeName">The type name.</param>
- /// <returns>The wrapped type.</returns>
- /// <seealso cref="AsDynamicType"/>
- public static dynamic GetDynamicType(this Assembly assembly, string typeName)
- {
- if (assembly == null)
- throw new ArgumentNullException(nameof(assembly));
- return assembly.GetType(typeName).AsDynamicType();
- }
- /// <summary>
- /// Tries to instantiate the type with the specified type name from the specified assembly instance using the specified constructor arguments.
- /// </summary>
- /// <param name="assembly">The assembly instance to search.</param>
- /// <param name="typeName">The full type name.</param>
- /// <param name="args">The arguments to pass to the constructor.</param>
- /// <returns></returns>
- /// <exception cref="MissingMethodException">Thrown when no suitable constructor can be found.</exception>
- public static dynamic CreateDynamicInstance(this Assembly assembly, string typeName, params object[] args)
- {
- if (args == null)
- throw new ArgumentNullException(nameof(args));
- return assembly.GetDynamicType(typeName).New(args);
- }
- }
- }
|