PageRenderTime 190ms CodeModel.GetById 179ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/src/LinFu.Reflection.Emit/ModuleDefinitionExtensions.cs

http://github.com/philiplaureano/LinFu
C# | 169 lines | 63 code | 12 blank | 94 comment | 1 complexity | 996ef63b79a89763eb5279173d585abb MD5 | raw file
  1using System;
  2using System.Linq;
  3using System.Reflection;
  4using Mono.Cecil;
  5using TypeAttributes = Mono.Cecil.TypeAttributes;
  6
  7namespace LinFu.Reflection.Emit
  8{
  9    /// <summary>
 10    ///     A class that provides helper extension methods
 11    ///     for the <see cref="ModuleDefinition" /> class.
 12    /// </summary>
 13    public static class ModuleDefinitionExtensions
 14    {
 15        /// <summary>
 16        ///     Defines a new class and adds it to the <paramref name="mainModule" /> module.
 17        /// </summary>
 18        /// <param name="mainModule">The module which will hold the new created type.</param>
 19        /// <param name="typeName">The name of the class to create.</param>
 20        /// <param name="namespaceName">The namespace that will contain the new class.</param>
 21        /// <param name="attributes">The <see cref="Mono.Cecil.TypeAttributes" /> for the given type.</param>
 22        /// <param name="baseType">The base class of the new type.</param>
 23        /// <returns>A <see cref="TypeDefinition" /> representing the new class being created.</returns>
 24        public static TypeDefinition DefineClass(this ModuleDefinition mainModule,
 25            string typeName, string namespaceName, TypeAttributes attributes,
 26            TypeReference baseType)
 27        {
 28            var resultType = new TypeDefinition(typeName, namespaceName,
 29                attributes, baseType);
 30
 31            mainModule.Types.Add(resultType);
 32            return resultType;
 33        }
 34
 35        /// <summary>
 36        ///     Imports a constructor with the given <paramref name="constructorParameters" />
 37        ///     into the target <paramref name="module" />.
 38        /// </summary>
 39        /// <typeparam name="T">The type that holds the target constructor</typeparam>
 40        /// <param name="module">The <see cref="ModuleDefinition" /> that will import the target constructor.</param>
 41        /// <param name="constructorParameters">
 42        ///     The list of <see cref="System.Type" /> objects that describe the signature of the
 43        ///     constructor.
 44        /// </param>
 45        /// <returns>A <see cref="MethodReference" /> that represents the constructor itself.</returns>
 46        public static MethodReference ImportConstructor<T>(this ModuleDefinition module,
 47            params Type[] constructorParameters)
 48        {
 49            return module.Import(typeof(T).GetConstructor(constructorParameters));
 50        }
 51
 52        /// <summary>
 53        ///     Imports a method with a particular <paramref name="methodName" /> from the <paramref name="declaringType" />
 54        ///     into the <paramref name="module">target module</paramref>.
 55        /// </summary>
 56        /// <param name="module">The <see cref="ModuleDefinition" /> instance that will import the actual method.</param>
 57        /// <param name="methodName">The name of the method being imported.</param>
 58        /// <param name="declaringType">The <see cref="System.Type" /> instance that holds the target method.</param>
 59        /// <returns>A <see cref="MethodReference" /> that represents the method being imported.</returns>
 60        public static MethodReference ImportMethod(this ModuleDefinition module, string methodName, Type declaringType)
 61        {
 62            return module.Import(declaringType.GetMethod(methodName));
 63        }
 64
 65        /// <summary>
 66        ///     Imports a method with a particular <paramref name="methodName" /> and <see cref="BindingFlags" /> from the
 67        ///     <paramref name="declaringType" />
 68        ///     into the <paramref name="module">target module</paramref>.
 69        /// </summary>
 70        /// <param name="module">The <see cref="ModuleDefinition" /> instance that will import the actual method.</param>
 71        /// <param name="methodName">The name of the method being imported.</param>
 72        /// <param name="declaringType">The <see cref="System.Type" /> instance that holds the target method.</param>
 73        /// <param name="flags">The <see cref="BindingFlags" /> that describes the visibility and behavior of the target method.</param>
 74        /// <returns>A <see cref="MethodReference" /> that represents the method being imported.</returns>
 75        public static MethodReference ImportMethod(this ModuleDefinition module, string methodName, Type declaringType,
 76            BindingFlags flags)
 77        {
 78            return module.Import(declaringType.GetMethod(methodName, flags));
 79        }
 80
 81        /// <summary>
 82        ///     Imports a method with a particular <paramref name="methodName" /> and <see cref="BindingFlags" /> from the
 83        ///     declaring type
 84        ///     into the <paramref name="module">target module</paramref>.
 85        /// </summary>
 86        /// <typeparam name="T">The target type that holds the target method.</typeparam>
 87        /// <param name="module">The <see cref="ModuleDefinition" /> instance that will import the actual method.</param>
 88        /// <param name="methodName">The name of the method being imported.</param>
 89        /// <returns>A <see cref="MethodReference" /> that represents the method being imported.</returns>
 90        public static MethodReference ImportMethod<T>(this ModuleDefinition module, string methodName)
 91        {
 92            return module.Import(typeof(T).GetMethod(methodName));
 93        }
 94
 95        /// <summary>
 96        ///     Imports a method with a particular <paramref name="methodName" />, <paramref name="parameterTypes" />, and
 97        ///     <see cref="BindingFlags" /> from the declaring type
 98        ///     into the <paramref name="module">target module</paramref>.
 99        /// </summary>
100        /// <typeparam name="T">The target type that holds the target method.</typeparam>
101        /// <param name="parameterTypes">The list of <see cref="Type" /> objects that describe the method signature.</param>
102        /// <param name="module">The <see cref="ModuleDefinition" /> instance that will import the actual method.</param>
103        /// <param name="methodName">The name of the method being imported.</param>
104        /// <returns>A <see cref="MethodReference" /> that represents the method being imported.</returns>
105        public static MethodReference ImportMethod<T>(this ModuleDefinition module, string methodName,
106            params Type[] parameterTypes)
107        {
108            return module.Import(typeof(T).GetMethod(methodName, parameterTypes));
109        }
110
111        /// <summary>
112        ///     Imports a method with a particular <paramref name="methodName" /> and <see cref="BindingFlags" /> from the
113        ///     declaring type.
114        ///     into the <paramref name="module">target module</paramref>.
115        /// </summary>
116        /// <typeparam name="T">The target type that holds the target method itself.</typeparam>
117        /// <param name="module">The <see cref="ModuleDefinition" /> instance that will import the actual method.</param>
118        /// <param name="methodName">The name of the method being imported.</param>
119        /// <param name="flags">The <see cref="BindingFlags" /> that describes the visibility and behavior of the target method.</param>
120        /// <returns>A <see cref="MethodReference" /> that represents the method being imported.</returns>
121        public static MethodReference ImportMethod<T>(this ModuleDefinition module,
122            string methodName, BindingFlags flags)
123        {
124            return module.Import(typeof(T).GetMethod(methodName, flags));
125        }
126
127        /// <summary>
128        ///     Imports a target of type <typeparamref name="T" /> into the
129        ///     <paramref name="module" /> instance.
130        /// </summary>
131        /// <typeparam name="T">The type that will be imported into the <see cref="ModuleDefinition" /> instance itself.</typeparam>
132        /// <param name="module">The module that will store the imported type.</param>
133        /// <returns>A <see cref="TypeReference" /> instance that represents the imported type.</returns>
134        public static TypeReference ImportType<T>(this ModuleDefinition module)
135        {
136            return module.Import(typeof(T));
137        }
138
139        /// <summary>
140        ///     Imports a <paramref name="targetType">target type</paramref> into the
141        ///     <paramref name="module" /> instance.
142        /// </summary>
143        /// <param name="targetType">The type that will be imported into the <see cref="ModuleDefinition" /> instance itself.</param>
144        /// <param name="module">The module that will store the imported type.</param>
145        /// <returns>A <see cref="TypeDefinition" /> instance that represents the imported type.</returns>
146        public static TypeReference ImportType(this ModuleDefinition module, Type targetType)
147        {
148            return module.Import(targetType);
149        }
150
151        /// <summary>
152        ///     Returns a <see cref="TypeDefinition" /> that matches the given <paramref name="typeName" />.
153        /// </summary>
154        /// <param name="module">The target module to search.</param>
155        /// <param name="typeName">The name of the target type.</param>
156        /// <returns>
157        ///     A type that matches the given type name. If the type cannot be found, then this method will return <c>null</c>
158        ///     .
159        /// </returns>
160        public static TypeDefinition GetType(this ModuleDefinition module, string typeName)
161        {
162            var result = (from TypeDefinition t in module.Types
163                where t.Name == typeName
164                select t).FirstOrDefault();
165
166            return result;
167        }
168    }
169}