gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:55:21 24 4
gpt4 key购买 nike

我正在尝试将整数数组从经典 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com