简体   繁体   English

如何使用javascript调用C#类的COM互操作

[英]How to call COM Interop of an C# Class,methods using javascript

I created COM Interop of an C# Class to access this interop usng javascript. 我创建了一个C#类的COM互操作,以使用JavaScript来访问此互操作。 The Class Contains an Addition() Method but the method can not be access in javascript. 该类包含一个Addition()方法,但是该方法无法在javascript中访问。

Code as Below 代码如下

Interface IMathInterface 接口IMathInterface

namespace CCWTEST
{
    [Guid("756892A0-1242-4BF7-984D-C13E68000E8E")]
    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IMathInterface
    {
        [DispId(1)]
        int Addition(int i, int j);
    }
}

Class ClassMath 类ClassMath

[Guid("CA9AD3A7-BD31-4BE2-A780-8864D493BA5F")]
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("CCWTEST.ClassMath")]
public class ClassMath : IMathInterface
{
    [ComVisible(true)]
    public int Addition(int x, int y)
    {
        return x + y;
    }
}

Then created snk file CCWMathTest.snk also register into GACUtil. 然后创建的snk文件CCWMathTest.snk也注册到GACUtil中。 then Create a tlb file using RegAsm command.All these thing is done successfully. 然后使用RegAsm命令创建一个tlb文件。所有这些操作均成功完成。

then create an web application to access an Class Method into javascript. 然后创建一个Web应用程序以将类方法访问javascript。

Code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <object classid="CLSID:CA9AD3A7-BD31-4BE2-A780-8864D493BA5F" height="0" width="0"
        id="obj7" name="obj7">
    </object>
       <title></title>
    <script type="text/javascript">
        window.onload = onLoad;
        function onLoad() {

            alert(obj7.Addition(3,1));

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>
</html>

while running this code it gives me an error "Object doesn't support property or method 'Addition'" How Can I access this method in Javascript using COM interop 在运行此代码时,它给我一个错误“对象不支持属性或方法'添加'”我如何使用COM互操作在Javascript中访问此方法

Why do you calling it from javascript? 你为什么用javascript调用它? Make a WebMethod on your page, which calls COM object, and then just call this mehod via AJAX from javascript. 在页面上创建一个WebMethod,该WebMethod调用COM对象,然后通过javascript通过AJAX调用此方法。 Like this: 像这样:

//server side
[WebMethod]
public static string DoSomething()
{
    //call COM
    return "something";
}

//client side
$.ajax({
    type: "POST",
    url: "page.aspx/DoSomething",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: OnSuccess,
    failure: function (response) {
        alert(response.d);
        },
        error: function (response) {
            alert(response.d);
        }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM