CreateDelegateTypeAndMethodInfo() private method

private CreateDelegateTypeAndMethodInfo ( ModuleBuilder moduleBuilder, TypeBuilder wrapperTypeBuilder, MethodInfo targetMethod, object target ) : void
moduleBuilder System.Reflection.Emit.ModuleBuilder
wrapperTypeBuilder System.Reflection.Emit.TypeBuilder
targetMethod System.Reflection.MethodInfo
target object
return void
Exemplo n.º 1
0
        // This is the main conversion function called from XlLibrary.RegisterMethods
        // targets may be null - the typical case
        public static List <XlMethodInfo> ConvertToXlMethodInfos(List <MethodInfo> methods, List <object> targets, List <object> methodAttributes, List <List <object> > argumentAttributes)
        {
            List <XlMethodInfo> xlMethodInfos = new List <XlMethodInfo>();

            // Set up assembly
            // Examine the methods, built the types and infos
            // Bake the assembly and export the function pointers

            AssemblyBuilder assemblyBuilder;
            ModuleBuilder   moduleBuilder;
            TypeBuilder     wrapperTypeBuilder;

            assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
                new AssemblyName("ExcelDna.DynamicDelegateAssembly"),
                AssemblyBuilderAccess.Run /*AndSave*/);
            moduleBuilder      = assemblyBuilder.DefineDynamicModule("DynamicDelegates");
            wrapperTypeBuilder = moduleBuilder.DefineType("WrapperType");

            for (int i = 0; i < methods.Count; i++)
            {
                MethodInfo    mi           = methods[i];
                object        target       = (targets == null) ? null : targets[i];
                object        methodAttrib = (methodAttributes != null && i < methodAttributes.Count) ? methodAttributes[i] : null;
                List <object> argAttribs   = (argumentAttributes != null && i < argumentAttributes.Count) ? argumentAttributes[i] : null;
                try
                {
                    XlMethodInfo xlmi = new XlMethodInfo(mi, target, methodAttrib, argAttribs);
                    // Skip if suppressed
                    if (xlmi.ExplicitRegistration)
                    {
                        Logger.Registration.Info("Suppressing due to ExplictRegistration attribute: '{0}.{1}'", mi.DeclaringType.Name, mi.Name);
                        continue;
                    }
                    // otherwise continue with delegate type and method building
                    xlmi.CreateDelegateTypeAndMethodInfo(moduleBuilder, wrapperTypeBuilder, mi, target);

                    // ... and add to list for further processing and registration
                    xlMethodInfos.Add(xlmi);
                }
                catch (DnaMarshalException e)
                {
                    Logger.Registration.Error(e, "Method not registered due to unsupported signature: '{0}.{1}'", mi.DeclaringType.Name, mi.Name);
                }
            }

            Type wrapperType = wrapperTypeBuilder.CreateType();

            foreach (XlMethodInfo xlmi in xlMethodInfos)
            {
                xlmi.CreateDelegateAndFunctionPointer(wrapperType);
            }

            //			assemblyBuilder.Save(@"ExcelDna.DynamicDelegateAssembly.dll");
            return(xlMethodInfos);
        }
Exemplo n.º 2
0
        // This is the main conversion function called from XlLibrary.RegisterMethods
        // targets may be null - the typical case
        public static List<XlMethodInfo> ConvertToXlMethodInfos(List<MethodInfo> methods, List<object> targets, List<object> methodAttributes, List<List<object>> argumentAttributes)
        {
            List<XlMethodInfo> xlMethodInfos = new List<XlMethodInfo>();

            // Set up assembly
            // Examine the methods, built the types and infos
            // Bake the assembly and export the function pointers

            AssemblyBuilder assemblyBuilder;
            ModuleBuilder moduleBuilder;
            TypeBuilder wrapperTypeBuilder;

            assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
                new AssemblyName("ExcelDna.DynamicDelegateAssembly"),
                AssemblyBuilderAccess.Run /*AndSave*/);
            moduleBuilder = assemblyBuilder.DefineDynamicModule("DynamicDelegates");
            wrapperTypeBuilder = moduleBuilder.DefineType("WrapperType");

            for (int i = 0; i < methods.Count; i++)
            {
                MethodInfo mi  = methods[i];
                object target = (targets == null) ? null : targets[i];
                object methodAttrib = (methodAttributes != null && i < methodAttributes.Count) ? methodAttributes[i] : null;
                List<object> argAttribs = (argumentAttributes != null && i < argumentAttributes.Count) ? argumentAttributes[i] : null;
                try
                {
                    XlMethodInfo xlmi = new XlMethodInfo(mi, target, methodAttrib, argAttribs);
                    // Skip if suppressed
                    if (xlmi.ExplicitRegistration)
                    {
                        Logger.Registration.Info("Suppressing due to ExplictRegistration attribute: '{0}.{1}'", mi.DeclaringType.Name, mi.Name);
                        continue;
                    }
                    // otherwise continue with delegate type and method building
                    xlmi.CreateDelegateTypeAndMethodInfo(moduleBuilder, wrapperTypeBuilder, mi, target);

                    // ... and add to list for further processing and registration
                    xlMethodInfos.Add(xlmi);
                }
                catch (DnaMarshalException e)
                {
                    Logger.Registration.Error(e, "Method not registered due to unsupported signature: '{0}.{1}'", mi.DeclaringType.Name, mi.Name);
                }
            }

            Type wrapperType = wrapperTypeBuilder.CreateType();
            foreach (XlMethodInfo xlmi in xlMethodInfos)
            {
                xlmi.CreateDelegateAndFunctionPointer(wrapperType);
            }

            //			assemblyBuilder.Save(@"ExcelDna.DynamicDelegateAssembly.dll");
            return xlMethodInfos;
        }