public static Type DefineDelegateType(string typeName, Type returnType, params Type[] parameterTypes)
        {
            DelegateInfo info = new DelegateInfo(returnType, parameterTypes);

            if (_typeCache.ContainsKey(info))
                return _typeCache[info];

            AppDomain currentDomain = AppDomain.CurrentDomain;
            string assemblyName = string.Format("{0}Assembly", typeName);
            string moduleName = string.Format("{0}Module", typeName);

            AssemblyName name = new AssemblyName(assemblyName);
            AssemblyBuilderAccess access = AssemblyBuilderAccess.RunAndSave;
            AssemblyBuilder assemblyBuilder = currentDomain.DefineDynamicAssembly(name, access);

            ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(moduleName, string.Format("{0}.mod", moduleName), true);
            TypeAttributes typeAttributes = TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.Public | TypeAttributes.AnsiClass | TypeAttributes.AutoClass;
            TypeBuilder typeBuilder = moduleBuilder.DefineType(typeName, typeAttributes, typeof(MulticastDelegate));

            // Delegate constructor
            ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.RTSpecialName |
                MethodAttributes.HideBySig | MethodAttributes.Public | MethodAttributes.SpecialName,
                CallingConventions.Standard,
                new Type[] { typeof(object), typeof(System.IntPtr) });

            constructorBuilder.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed);

            // Define the Invoke method with a signature that matches the parameter types
            MethodAttributes methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual;
            MethodBuilder methodBuilder = typeBuilder.DefineMethod("Invoke", methodAttributes, returnType, parameterTypes);
            methodBuilder.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed);

            #region Define the Begin/EndInvoke methods for async callback support
            methodBuilder = typeBuilder.DefineMethod("BeginInvoke", methodAttributes, typeof(System.IAsyncResult),
                new Type[] { typeof(int), typeof(AsyncCallback), typeof(object) });
            methodBuilder.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed);

            methodBuilder = typeBuilder.DefineMethod("EndInvoke", methodAttributes, typeof(void),
                new Type[] { typeof(IAsyncResult) });
            methodBuilder.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed);
            #endregion

            Type result = typeBuilder.CreateType();

            // Cache the result
            if (result != null)
                _typeCache[info] = result;

            return result;
        }
        private static bool CompareParameters(DelegateInfo info, Type[] parameterTypes)
        {
            if (info.Parameters == null && parameterTypes == null)
                return true;

            if (info.Parameters.Length != parameterTypes.Length)
                return false;

            for (int position = 0; position < parameterTypes.Length; position++)
            {
                if (info.Parameters[position] != parameterTypes[position])
                    return false;
            }

            return true;
        }
Example #3
0
 private static bool Compare(DelegateInfo lhs, DelegateInfo rhs)
 {
     return(lhs.ReturnType == rhs.ReturnType &&
            CompareParameters(lhs, rhs.Parameters));
 }
Example #4
0
        public static Type DefineDelegateType(string typeName, Type returnType, params Type[] parameterTypes)
        {
            var info = new DelegateInfo(returnType, parameterTypes);

            if (TypeCache.ContainsKey(info))
            {
                return(TypeCache[info]);
            }

            var currentDomain = AppDomain.CurrentDomain;
            var assemblyName  = string.Format("{0}Assembly", typeName);
            var moduleName    = string.Format("{0}Module", typeName);

            var name            = new AssemblyName(assemblyName);
            var access          = AssemblyBuilderAccess.RunAndSave;
            var assemblyBuilder = currentDomain.DefineDynamicAssembly(name, access);

            var moduleBuilder = assemblyBuilder.DefineDynamicModule(moduleName, string.Format("{0}.mod", moduleName),
                                                                    true);
            var typeAttributes = TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.Public |
                                 TypeAttributes.AnsiClass | TypeAttributes.AutoClass;
            var typeBuilder = moduleBuilder.DefineType(typeName, typeAttributes, typeof(MulticastDelegate));

            // Delegate constructor
            var constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.RTSpecialName |
                                                                   MethodAttributes.HideBySig | MethodAttributes.Public |
                                                                   MethodAttributes.SpecialName,
                                                                   CallingConventions.Standard,
                                                                   new[] { typeof(object), typeof(IntPtr) });

            constructorBuilder.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed);

            // Define the Invoke method with a signature that matches the parameter types
            var methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot |
                                   MethodAttributes.Virtual;

            var methodBuilder = typeBuilder.DefineMethod("Invoke", methodAttributes, returnType, parameterTypes);

            methodBuilder.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed);

            #region Define the Begin/EndInvoke methods for async callback support

            methodBuilder = typeBuilder.DefineMethod("BeginInvoke", methodAttributes, typeof(IAsyncResult),
                                                     new[] { typeof(int), typeof(AsyncCallback), typeof(object) });
            methodBuilder.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed);

            methodBuilder = typeBuilder.DefineMethod("EndInvoke", methodAttributes, typeof(void),
                                                     new[] { typeof(IAsyncResult) });
            methodBuilder.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed);

            #endregion

            var result = typeBuilder.CreateType();

            // Cache the result
            if (result != null)
            {
                TypeCache[info] = result;
            }

            return(result);
        }
 private static bool Compare(DelegateInfo lhs, DelegateInfo rhs)
 {
     return lhs.ReturnType == rhs.ReturnType &&
         CompareParameters(lhs, rhs.Parameters) == true;
 }