[英]Method Overloading in Excel COM Interop
我在這里遇到了很多方法重載的問題,並且不知道為什么每次只調用一個方法,而不管我傳入的參數數量如何。 下面是示例代碼。
[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 中對其進行測試時,基本方法運行良好,並且可以獲得預期值。 但是,如果我使用方法重載嘗試其余兩個函數,它將失敗。 他們只是因為某種原因沒有被召喚。 我在代碼中遺漏了什么嗎? 有人可以幫我嗎? 非常感謝。
編輯:
通過一些實驗,我發現只能識別一種方法,通常是第一種方法。 因此,如果我在基方法和重載方法 1 之間交換順序,則將調用重載方法 1 而不是基方法。 看起來整個班級都被困在第一種方法中而無法繼續。
COM Interop 不支持重載:請參閱此 MSDN 文章中的“重載”部分。
類型庫導出器將通過附加前綴(例如 _2)來重命名重載的成員。
Excelfriend,您不能對工作表函數進行多態化。 一個名為
GETSALESFORWEEK
工作表函數只能有一個入口點。 如果您提供兩個,比如GETSALESFORWEEK(int WeekNumber)
和GETSALESFORWEEK(string WeekName)
那么 Excel 2007 會將這些函數公開為GETSALESFORWEEK
和GETSALESFORWEEK_2
。
來源: http : //blogs.msdn.com/b/gabhan_berry/archive/2008/04/07/writing-custom-excel-worksheet-functions-in-c_2d00_sharp.aspx
好吧,我想這回答了我的問題。 遺憾的是,自定義 Excel 函數沒有方法重載:(
解決方案:
[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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.