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.
精彩评论