Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ExcelDNA在运行时动态注册UDF_C#_Excel_User Defined Functions_Excel Interop_Excel Dna - Fatal编程技术网

C# ExcelDNA在运行时动态注册UDF

C# ExcelDNA在运行时动态注册UDF,c#,excel,user-defined-functions,excel-interop,excel-dna,C#,Excel,User Defined Functions,Excel Interop,Excel Dna,我的目标是在运行时在ExcelDNA加载项中动态构建和注册Excel用户定义函数 由ExcelDNA作者提供,重点介绍如何从简单的C代码字符串编译UDF 如您所见,此代码是通过在加载项的AutoOpen方法中调用RegisterMyCass来执行的;一切都很完美 但是,如果将RegisterMyClass方法移动到功能区按钮的操作方法中,则动态UDF的注册将不起作用,并导致以下错误: Registration [Error] xlfRegister call failed for functio

我的目标是在运行时在ExcelDNA加载项中动态构建和注册Excel用户定义函数

由ExcelDNA作者提供,重点介绍如何从简单的C代码字符串编译UDF

如您所见,此代码是通过在加载项的AutoOpen方法中调用RegisterMyCass来执行的;一切都很完美

但是,如果将RegisterMyClass方法移动到功能区按钮的操作方法中,则动态UDF的注册将不起作用,并导致以下错误:

Registration [Error] xlfRegister call failed for function or command: 'MyDynamicAdd'
事实上,任何对ExcelIntegration.RegisterMethods的调用似乎都会在上述错误消息中失败,除非它们是从AutoOpen方法中调用的

我的问题是: 如何在运行时通过单击功能区按钮动态注册新的UDF

为完整起见,参考了Gist代码:
注册函数的代码需要位于C API可用的上下文中。它在功能区回调或任何其他COM事件处理程序中都不起作用

切换到C API可用的宏上下文的一个选项是调用ExcelAsyncUtil.QueueAsMacro帮助程序,并在传入的代理中运行注册代码

<DnaLibrary Name="ExcelDna Test Dynamic Method" Language="C#">
<Reference Name="System.Windows.Forms" />
    <![CDATA[
    using System;
    using System.CodeDom.Compiler;
    using System.Collections.Generic;
    using System.IO;
    using System.Reflection;
    using System.Windows.Forms;
    using Microsoft.CSharp;
    using ExcelDna.Integration;

    public class Test : IExcelAddIn
    {
        // Just to test that we are loaded.
        public static double MyAdd(double d1, double d2)
        {
            return d1 + d2;
        }

        public void AutoOpen()
        {
            RegisterMyClass();
        }

        public void AutoClose()
        {
        }

        private void RegisterMyClass()
        {
            string code = 
                @"
                public class Script 
                { 
                    public static double MyDynamicAdd(double d1, double d2)
                    {
                        return d1 + d2;
                    }
                }";

        CompilerParameters cp = new CompilerParameters();
        cp.GenerateExecutable = false;
        cp.GenerateInMemory = true;
        cp.TreatWarningsAsErrors = false;
        cp.ReferencedAssemblies.Add("System.dll"); //, "System.Windows.Forms.dll", "ExcelDna.Integration.dll" );
        CSharpCodeProvider provider = new CSharpCodeProvider();
        CompilerResults cr = provider.CompileAssemblyFromSource(cp, new string[] { code });
        if (!cr.Errors.HasErrors)
        {
            Assembly asm = cr.CompiledAssembly;
            Type[] types = asm.GetTypes();
            List<MethodInfo> methods = new List<MethodInfo>();

            // Get list of MethodInfo's from assembly for each method with ExcelFunction attribute
            foreach (Type type in types)
            {
                foreach (MethodInfo info in type.GetMethods(BindingFlags.Public | BindingFlags.Static))
                {
                    methods.Add(info);
                }
            }
            Integration.RegisterMethods(methods);
        }
        else
        {
            MessageBox.Show("Errors during compile!");
        }
    }
    }
]]>
</DnaLibrary>