处理事件在.net上通过com在vbs中暴露

时间:2016-08-10 11:26:53

标签: c# .net com

我想在.net中向com公开一些事件。但是有些不对劲

这是C#代码:

[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IEventConnectionPoint
{
    void OnRead(string msg);
}

[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ITest
{
    void TestEvent();
}


[ComVisible(true)]
[ProgId("fst.eventtest")]
[ComSourceInterfaces(typeof(IEventConnectionPoint))]
[ComDefaultInterface(typeof(ITest))]
[ClassInterface(ClassInterfaceType.None)]
public class ComEventTest : ITest
{
    [ComVisible(false)]
    public delegate void OnReadHandler(string msg);

    public event OnReadHandler OnRead;

    private int _count;
    public void TestEvent()
    {
        MessageBox.Show($"{_count} at .net 1,typeof(onread) is {OnRead?.ToString()}");
        try
        {
            OnRead?.Invoke("hello");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), "Error");
        }
        MessageBox.Show($"{_count} at .net 2");
    }
}

这是vbs中的代码:

function com_OnRead(msg)

end function

set com=wscript.createobject("fst.eventtest","com_") '"com_"the same as "com"
'wscript.connectobject com,"com"
com.testEvent

当我运行vbs代码时,出现错误:未实现方法或操作(在.net中)。

高兴的帮助我,出了什么问题?

0 个答案:

没有答案