[英]Retrieve string array from COM object using Classic ASP
我有一個 .NET class 它包含一個簡單的字符串數組,可通過訪問器方法獲得,如下所示;
namespace Foo.Bar {
[ComVisible(true)]
[Guid("642279A0-85D4-4c7a-AEF5-A9FAA4BE85E5")]
public class MyClass {
private string[] _myArray;
public MyClass() { }
public MyClass(string[] myArray) {
_myArray = myArray;
}
public string[] MyArray {
get { return _myArray; }
}
}
}
我使用 Classic ASP 使用這個 class;
Dim foo
Set foo = Server.CreateObject("Foo.Bar.MyClass")
if IsArray(foo.MyArray) then Response.Write("IsArray") & "<br />"
Response.Write(typename(foo.MyArray)) & "<br />"
Response.Write(UBound(foo.MyArray)) & "<br />"
這導致;
IsArray
String()
1
但是,當我嘗試使用訪問數組的內容時;
Response.Write(foo.MyArray(0)) & "<br />"
我明白了;
Microsoft VBScript 運行時 (0x800A01C2) arguments 的編號錯誤或屬性分配無效:“MyArray”
任何幫助深表感謝。
編輯這是為了在消化給出的答案后提供更多信息(謝謝)
將 MyArray 屬性的實現更改為;
public object[] MyArray {
get { return (object[])_myArray; }
}
然后我收到以下錯誤,
Microsoft VBScript 運行時 (0x800A000D) 類型不匹配:'MyArray'
所以我嘗試將每個字符串單獨轉換為 object;
public object[] MyArray {
get {
object[] tmp = new object[_myArray.Count()];
for (int x = 0; x < _myArray.Count(); x++) {
tmp[x] = (object)_myArray[x];
}
return tmp;
}
}
然后我回到,
Microsoft VBScript 運行時 (0x800A01C2) arguments 的編號錯誤或屬性分配無效:“MyArray”
在如何正確編組 VB-Script arrays 到 COM 組件的幫助下編輯最終解決方案
C#
public object MyArray {
get { return _myArray.Cast<object>().ToArray(); }
}
VBScript
Dim foo
Set foo = Server.CreateObject("Foo.Bar.MyClass")
bar = foo.MyArray
Response.Write bar(0)
關鍵是公開object
而不是object[]
並且正如 AnthonyWJones 建議的那樣,在使用之前將數組分配給變量。
再次感謝。
問題是VBScript實際上不能使用String
數組。 它只能使用Variant
數組。
嘗試更改MyClass
以顯示object[]
。
VBScript 不理解通用的 collections,例如 List,它也不理解字符串 arrays。
我在我的接口 class 中寫了一個公共 function 以將任何通用 collections 轉換為 Z57A97A39435CFBCDFED96E03F2A
public ArrayList toArrayList(IEnumerable collection)
{
var arrayList = new ArrayList();
foreach (object element in collection)
{
arrayList.Add(element);
}
return arrayList;
}
然后可以像這樣在 VBScript 中使用此代碼
dim connector
set connector = model.getRelationByID(connectorID)
'get the related elements
dim relatedElements
set relatedElements = model.toArrayList(connector.relatedElements)
addRelatedElementoAutoDiagram relatedElements(0), relatedElements(1), model
這種方法的優點是我不需要更改 C# 中任何方法或屬性的簽名,但我仍然可以在 VBScript 中使用它們
除了安東尼的建議
我不確定它是最好的方法,但在過去我使用類似下面的代碼來處理一維數組。
public object MyArray(int ix = -1){
string[] tmp = new string[] {"one", "two", "3", "4"};
return (ix == -1) ? (object)tmp : tmp[ix];
}
在ASP中:
Response.Write(TypeName(foo.MyArray)) 'string()
Response.Write(TypeName(foo.MyArray(0))) 'string
此代碼演示了如何在COM和ASP之間處理數組:
<% @Language="VBScript" %>
<% Option Explicit %>
<%
Dim tcs
Dim rc
Dim vntInput(0,4)
Dim i
vntInput(0,0) = Request.QueryString("strUser")
vntInput(0,1) = Request.QueryString("intCreate")
vntInput(0,2) = Request.QueryString("intDelete")
vntInput(0,3) = Request.QueryString("intModify")
vntInput(0,4) = Request.QueryString("intView")
Set tcs = Server.CreateObject("TestCases.ArrayFailure")
rc = tcs.AcceptArray(vntInput)
For i = 0 to UBound(vntInput, 2)
Response.write "Loop Count " & i & " " & vntInput(0,i) & "<BR>"
Next
%>
這是我找到此代碼的文章的鏈接:
http://202.102.233.250/b2000/ASP/articles/component/pv990826.htm
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.