我使用COM Interop在C#中编写了一个ActiveX控件来公开方法/属性。
[ComVisible(true)]
class COMClass:ICOMClass
{
public string methodA()
{
string str = "abc";
if(str != "abcd")
throw new Exception("invalid string");
return str;
}
}
[ComVisible(true)]
interface ICOMClass
{
string methodA();
}
有没有办法处理从javascript中的C#抛出的异常?我看了一遍,但找不到任何东西?
例如
var x = new ActiveXObject("COMClass");
try{
x.methodA
}
catch(e) {
alert(e);
}
答案 0 :(得分:1)
致电alert(e.message)
对我有用。
确保您的ActiveX类正在实施IObjectSafety
:
using System;
using System.Runtime.InteropServices;
[ComImport()]
[Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IObjectSafety
{
[PreserveSig()]
int GetInterfaceSafetyOptions(ref Guid riid, out int pdwSupportedOptions, out int pdwEnabledOptions);
[PreserveSig()]
int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions);
}