简体   繁体   English

将自定义类从.Net ActiveX控件(DLL)传递给Javascript

[英]Passing a custom class from a .Net ActiveX Control (DLL) to Javascript

I currently have an dll created in .Net 2.0 that has a COM visible component that is used as an ActiveX object on a webpage within IE. 我目前在.Net 2.0中创建了一个DLL,该DLL具有COM可见组件,该组件用作IE中网页上的ActiveX对象。

The concept works fine, calling function, raising events, passing variables back and forth. 这个概念很好用,可以调用函数,引发事件,来回传递变量。 The problem comes with complex classes of information. 问题来自复杂的信息类别。

For example, I have this class: 例如,我有这个课:

public class ClientInfo {
    public ClientInfo() { }

    public ClientInfo(DataRow dr)
    {
        ClientName = dr["Name"].ToString();
        Address = dr["Address1"].ToString();
    }

    public string ClientName;
    public string Address;
}

Which is simple enough. 这很简单。 I then have a function that builds an array to be returned that is made of the aforementioned class: 然后,我有一个函数来构建要返回的数组,该数组由上述类组成:

ArrayList arr = new ArrayList();

foreach (DataRow r in dsClients.Tables[0].Rows)
{
    arr.Add(new ClientInfo(r));
}

return arr.ToArray();

From javascript when this function is called, the return is undefined. 从javascript调用此函数时,返回值是不确定的。 It works fine when called from another .Net project (that consists of a simple button to test this very issue). 当从另一个.Net项目中调用时,它工作正常(该项目由一个简单的按钮来测试此问题)。

It seems I somehow need to convert the return object to something more accessible via javascript (JSON?), or possibly define the type of return variable within javascript. 似乎我需要以某种方式将返回对象转换为可通过javascript(JSON?)访问的对象,或者可能在javascript中定义返回变量的类型。

Any help would be appreciated. 任何帮助,将不胜感激。

EDIT: Of course, I can't use serialization because that isn't included until .Net 3.5, and our target is 2.0 编辑:当然,我不能使用序列化,因为直到.Net 3.5才包括在内,而我们的目标是2.0

Json is just text so you should be able to create your own json. Json只是文本,因此您应该能够创建自己的json。

Check out this link for an implementation http://geekswithblogs.net/Mochalogic/articles/103330.aspx 查看此链接以获取实现http://geekswithblogs.net/Mochalogic/articles/103330.aspx

Or maybe try json.net enter link description here 或者尝试json.net 在此处输入链接描述

Once you have the json on your client browser all you need to do to convert it to an object is call eval. 将json放在客户端浏览器上后,将其转换为对象所需要做的就是调用eval。 Have a look at Douglas Cockford's website for a more robust json eval solution. 请访问Douglas Cockford的网站,以获得更强大的json评估解决方案。

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

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