c# - 如何将整数数组从经典 asp 传递到 c# COM 对象?

标签 c# com asp-classic

我正在尝试将整数数组从经典 ASP 传递到用 C# 创建的 DLL。

我有以下 C# 方法:

public int passIntArray(object arr)
{
    int[] ia = (int[])arr;
    int sum = 0;
    for (int i = 0; i < ia.Length; i++)
        sum += ia[i];

    return sum;
}

我尝试了多种将 arr 转换为 int[] 的方法,但都没有成功。我的 asp 代码是:

var arr = [1,2,3,4,5,6];
var x = Server.CreateObject("dllTest.test");
Response.Write(x.passIntArray(arr));

我目前遇到以下错误:

Unable to cast COM object of type 'System.__ComObject' to class type 'System.Int32[]'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

谁能告诉我怎么做或告诉我不能做?

使用这个非常有用的页面上的代码 http://www.add-in-express.com/creating-addins-blog/2011/12/20/type-name-system-comobject/我设法发现传递的参数的类型是“JScriptTypeInfo”,如果它有用的话。

如果我添加:

foreach (object m in arr.GetType().GetMembers())
    // output m

我得到以下输出:

System.Object GetLifetimeService()
System.Object InitializeLifetimeService()
System.Runtime.Remoting.ObjRef CreateObjRef(System.Type)
System.String ToString()
Boolean Equals(System.Object)
Int32 GetHashCode()
System.Type GetType()

最佳答案

SO item I suggested was a duplicate 中所述,您可以这样更改您的 ASP 代码:

function getSafeArray(jsArr) 
{
  var dict = new ActiveXObject("Scripting.Dictionary");     
  for (var i = 0; i < jsArr.length; i++)     
    dict.add(i, jsArr[i]);     
  return dict.Items(); 
} 
var arr = [1,2,3,4,5,6]; 
var x = Server.CreateObject("dllTest.test"); 
Response.Write(x.passIntArray(getSafeArray(arr))); 

您还应该将 C# 方法签名更改为:

public int passIntArray(object[] arr) // EDITED: 17-Sept

public int passIntArray([MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_I4)]  int[] arr)

重点是你并不是真的想从 JavaScript 到 C#,你是从 JavaScript 到 COM:你只能连接 C# DLL,因为它是 ComVisible 并且在 COM 注册表中注册了一个 ProgID Server.CreateObject 可以查找。通过签名更改,您的 DLL 的 COM 公开接口(interface)将期望收到一个非托管的 SAFEARRAY。上面的脚本代码是一种让 JavaScript 提供的方法,使用 COM Scripting.Dictionary 作为一种自定义编码(marshal)拆收器。

关于c# - 如何将整数数组从经典 asp 传递到 c# COM 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12421892/

相关文章:

c# - 返回一个已经从 WCF 格式化为 JSON 的字符串

c# - 异常处理 : user defined exception benefits

c++ - 将 BSTR 转换为 LPCWSTR

web-services - 通过soap向webservice发出请求并得到响应经典asp

asp-classic - asp到asp.net的迁移(c#.net)

UNC 共享 : is it problematic? 上的 IIS 站点

c# - 动态绑定(bind)到资源的 "Path"

c# - 是否可以在没有 World 对象的情况下存储 Box2D/Farseer Body 对象?

delphi - 将 COM 对象接口(interface)从 C 转换为 Delphi

azure - 在 Azure 上注册 COM/dll