Excel COM Interop中的方法重载 [英] Method Overloading in Excel COM Interop

查看:135
本文介绍了Excel COM Interop中的方法重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里方法重载时遇到很多麻烦,不知道为什么无论我传入多少参数,每次都只调用一个方法。下面是示例代码。

I am having a lot of troubles with method overloading here and have no idea why only one method gets called each time regardless the number of parameters I pass into. Below is the sample code.

[ClassInterface(ClassInterfaceType.AutoDual), ComVisible(true)]
public class myClass
{
    //constructor
    public myClass() {}

    //base method
    public string myfunction(string id, string pk) {return results;}

    //overloading method 1
    public string myfunction(string id, string pk, string param1) { return results;}

    //overloading method 2
    public string myfunction(string id, string pk, string param1, string param2) {return results;}



    [ComRegisterFunctionAttribute]
        public static void RegisterFunction(Type type)
        {

            Registry.ClassesRoot.CreateSubKey(
              GetSubKeyName(type, "Programmable"));
            RegistryKey key = Registry.ClassesRoot.OpenSubKey(
              GetSubKeyName(type, "InprocServer32"), true);
            key.SetValue("",
              System.Environment.SystemDirectory + @"\mscoree.dll",
              RegistryValueKind.String);
        }
        [ComUnregisterFunctionAttribute]
        public static void UnregisterFunction(Type type)
        {

            Registry.ClassesRoot.DeleteSubKey(
              GetSubKeyName(type, "Programmable"), false);
        }
        private static string GetSubKeyName(Type type,
          string subKeyName)
        {
            System.Text.StringBuilder s =
              new System.Text.StringBuilder();
            s.Append(@"CLSID\{");
            s.Append(type.GUID.ToString().ToUpper());
            s.Append(@"}\");
            s.Append(subKeyName);
            return s.ToString();
        }

}

因此,当我在Excel中对其进行测试时,基本方法工作正常,我可以获得预期的值。但是,如果我使用方法重载尝试了其余两个功能,它将失败。他们只是因为某种原因而没有得到电话。我在代码中缺少什么吗?谁能帮我吗?非常感谢。

So when I tested it in Excel, the base method worked fine and I could get the expected value. However, it would fail if I tried the rest two functions using method overloading. They simply didn't get called for some reason. Am I missing something in the code? Can anyone help me please? Thanks very much.

编辑:

通过一些实验,我发现只有一种方法可以识别,通常是第一种方法。因此,如果我在基本方法和重载方法1之间交换顺序,则将调用重载方法1而不是基本方法。看起来整个类都陷入了第一种方法,并且不会继续进行下去。

Through some experiments, it occurs to me that there only 1 method can be recognized, which normally is the first method. So if I swap the order between base method and overloading method 1, the overloading method 1 will be called instead of the base method. Looks like the whole class just gets stuck in the first method and wouldn't go on.

推荐答案


Excelfriend,您无法对工作表函数进行多态化。一个名为 GETSALESFORWEEK
工作表函数只能有一个
入口点。如果提供两个,请说 GETSALESFORWEEK(int WeekNumber)
GETSALESFORWEEK(string WeekName),然后使用Excel 2007会将这些
函数公开为 GETSALESFORWEEK GETSALESFORWEEK_2

Excelfriend, you can't polymorphize a a worksheet function. A worksheet function called, say, GETSALESFORWEEK can only have one entry point. If you provide two, say GETSALESFORWEEK(int WeekNumber) and GETSALESFORWEEK(string WeekName) then Excel 2007 will expose these functions as GETSALESFORWEEK and GETSALESFORWEEK_2.

来源: http://blogs.msdn.com/b/gabhan_berry/archive/2008/04/07/writing-custom-excel-worksheet-functions-in-c_2d00_sharp.aspx

好吧,我猜这回答了我的问题。可悲的是,自定义Excel函数没有方法重载:(

Well I guess that answers my questions. Sadly, no method overloading for custom Excel functions :(

解决方案:

[ClassInterface(ClassInterfaceType.AutoDual), ComVisible(true)]
public class myClass
{
    //constructor
    public myClass() {}

    //base method
    /**public string myfunction(string id, string pk) {return results;}**/

    //overloading method 1
    /**public string myfunction(string id, string pk, string param1) { return results;}**/

    //just use 1 method, but make the last two arguments optional by setting default values
    public string myfunction(string id, string pk, string param1 = "", string param2 = "") {return results;}

    [ComRegisterFunctionAttribute]
    public static void RegisterFunction(Type type)
    {
        Registry.ClassesRoot.CreateSubKey(
          GetSubKeyName(type, "Programmable"));
        RegistryKey key = Registry.ClassesRoot.OpenSubKey(
          GetSubKeyName(type, "InprocServer32"), true);
        key.SetValue("",
          System.Environment.SystemDirectory + @"\mscoree.dll",
          RegistryValueKind.String);
    }

    [ComUnregisterFunctionAttribute]
    public static void UnregisterFunction(Type type)
    {
        Registry.ClassesRoot.DeleteSubKey(
          GetSubKeyName(type, "Programmable"), false);
    }

    private static string GetSubKeyName(Type type, string subKeyName)
    {
        System.Text.StringBuilder s = new System.Text.StringBuilder();
        s.Append(@"CLSID\{");
        s.Append(type.GUID.ToString().ToUpper());
        s.Append(@"}\");
        s.Append(subKeyName);
        return s.ToString();
    }
}

这篇关于Excel COM Interop中的方法重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆