简体   繁体   English

如何将SAFEARRAY从C#返回到COM(Javascript)?

[英]How to Return a SAFEARRAY from C# to COM(Javascript)?

This Is my C# code my GetChannelSample() method return an int[] array I want to access this array into javascript but I dont know how to do this? 这是我的C#代码我的GetChannelSample()方法return an int[] array我想将此数组访问到javascript中,但是我不知道该怎么做?

[Guid("4794D615-BE51-4a1e-B1BA-453F6E9337C4")] 
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
class Test:IComOjbect
{
            private int[] nAllData;
            public int[] GetChannelSample(int channelIndex)
            {
             //Some Logic here that will return integer type of array{1,12,15,48,1452,45,100,01}
              return nAllData;
            }
}

[Guid("4B3AE7D8-FB6A-4558-8A96-BF82B54F329C")]
[ComVisible(true)]
public interface IComOjbect
 {
    [DispId(0x10000008)]
    int[] GetChannelSample(int channelIndex);
 }

I for this I created the COM Component using Gacutil and Regasm command so that com componant can easly access in javascript but I dont know how to return if my C# method retrn int[] array and access it using javascript array. 为此,我使用Gacutil和Regasm命令创建了COM组件,以便com组件可以轻松地在javascript中访问,但是我不知道如何返回C#方法retrn int []数组并使用javascript数组访问它。

If this is some kind of the handler you can just serialize the result to JSON and deserialize it on the client: 如果这是某种处理程序,则只需将结果序列化为JSON并在客户端上反序列化即可:

using JSON.NET 使用JSON.NET

string json = JsonConvert.SerializeObject(GetChannelSample(channelIndex));
this.Context.Response.ContentType = "text/plain";
this.currentContext.Response.Write(json);

or Javascript Serializer 或Javascript序列化器

JavascriptSerializer ser = new JavaScriptSerializer();
string json = ser.Serialize(GetChannelSample(channelIndex));
this.Context.Response.ContentType = "text/plain";
this.currentContext.Response.Write(json);

on the client: 在客户端上:

function onAjaxSuccess (data){
    var myArr = JSON.parse(data);
}

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

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