简体   繁体   中英

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.

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. It works fine when called from another .Net project (that consists of a simple button to test this very issue).

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.

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

Json is just text so you should be able to create your own json.

Check out this link for an implementation http://geekswithblogs.net/Mochalogic/articles/103330.aspx

Or maybe try json.net enter link description here

Once you have the json on your client browser all you need to do to convert it to an object is call eval. Have a look at Douglas Cockford's website for a more robust json eval solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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