<tfoot id='xdmeY'></tfoot>
      <bdo id='xdmeY'></bdo><ul id='xdmeY'></ul>

    <legend id='xdmeY'><style id='xdmeY'><dir id='xdmeY'><q id='xdmeY'></q></dir></style></legend>

      <i id='xdmeY'><tr id='xdmeY'><dt id='xdmeY'><q id='xdmeY'><span id='xdmeY'><b id='xdmeY'><form id='xdmeY'><ins id='xdmeY'></ins><ul id='xdmeY'></ul><sub id='xdmeY'></sub></form><legend id='xdmeY'></legend><bdo id='xdmeY'><pre id='xdmeY'><center id='xdmeY'></center></pre></bdo></b><th id='xdmeY'></th></span></q></dt></tr></i><div id='xdmeY'><tfoot id='xdmeY'></tfoot><dl id='xdmeY'><fieldset id='xdmeY'></fieldset></dl></div>

      <small id='xdmeY'></small><noframes id='xdmeY'>

        如何正确编组 VB 脚本数组进出用 C# 编写的 COM 组件

        时间:2023-10-06
        <tfoot id='7nLbw'></tfoot>
          <tbody id='7nLbw'></tbody>

            <bdo id='7nLbw'></bdo><ul id='7nLbw'></ul>

              • <small id='7nLbw'></small><noframes id='7nLbw'>

              • <legend id='7nLbw'><style id='7nLbw'><dir id='7nLbw'><q id='7nLbw'></q></dir></style></legend>

                <i id='7nLbw'><tr id='7nLbw'><dt id='7nLbw'><q id='7nLbw'><span id='7nLbw'><b id='7nLbw'><form id='7nLbw'><ins id='7nLbw'></ins><ul id='7nLbw'></ul><sub id='7nLbw'></sub></form><legend id='7nLbw'></legend><bdo id='7nLbw'><pre id='7nLbw'><center id='7nLbw'></center></pre></bdo></b><th id='7nLbw'></th></span></q></dt></tr></i><div id='7nLbw'><tfoot id='7nLbw'></tfoot><dl id='7nLbw'><fieldset id='7nLbw'></fieldset></dl></div>

                1. 本文介绍了如何正确编组 VB 脚本数组进出用 C# 编写的 COM 组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在用 C# (.Net 4.0) 构建一个 COM 对象,以用于经典的 asp 站点.现在我想知道在组件和asp站点之间来回编组VB-Script数组(单维和多维)的正确方法是什么?非常感谢您提供代码示例.

                  I'm building a COM object in C# (.Net 4.0) to be used in an classic asp site. Now I'd like to know what's the proper way to marshal VB-Script arrays (single and multidimensional) back and forth between the component and the asp site? A code sample would be highly appreciated.

                  推荐答案

                  VBScript 只喜欢处理包含 VARIANTS 的 SAFEARRAY.它喜欢在 COM 方法或属性的 VARIANTS 中传递这些参数.因此,您需要构造一个包含 VARIANT 类型的 SAFEARRAY 的 VARIANT 属性.以下 C# 代码执行此操作.首先只使用一个简单的对象数组,然后还展示了我们可以将任何其他托管类型的数组转换为对象数组,这样编组代码将为我们将其转换为 SAFEARRAY 的 VARIANT.

                  VBScript only likes to handle SAFEARRAY's that contain VARIANTS. And it likes to have these passed arround in VARIANTS on the COM methods or properties. So you need to construct a VARIANT property that contains a SAFEARRAY of VARIANT type. The following C# code does this. First using just a plain array of objects and then also showing we can cast an array of any other managed type into an array of objects such that the marshalling code will convert this into a SAFEARRAY of VARIANTs for us.

                  using System;
                  using System.Runtime.InteropServices;
                  using System.Linq;
                  
                  namespace StackOverflow
                  {
                      [ComVisible(true)]
                      [Guid("2F4C19A6-9BB9-4ACF-90D1-BAF48696740A")]
                      [InterfaceType(ComInterfaceType.InterfaceIsDual)]
                      public interface IMyArrayDemo
                      {
                          [DispId(1)]
                          int Count
                          {
                              [return: MarshalAs(UnmanagedType.I4)]
                              get;
                          }
                          [DispId(2)]
                          object Data
                          {
                              [return: MarshalAs(UnmanagedType.Struct, SafeArraySubType = VarEnum.VT_ARRAY)]
                              get;
                          }
                          [DispId(3)]
                          object Names
                          {
                              [return: MarshalAs(UnmanagedType.Struct, SafeArraySubType = VarEnum.VT_ARRAY)]
                              get;
                          }
                      }
                  
                      [ComVisible(true)]
                      [Guid("7EF75834-22BE-4861-879B-EA0CE20E46E9")]
                      [ClassInterface(ClassInterfaceType.None)]
                      [ProgId("StackOverflow.MyArrayDemo")]
                      public class MyArrayDemo : IMyArrayDemo
                      {
                          object[] mData = new object[10] { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 };
                          string[] mNames = new string[5] {"one", "two", "three", "four", "five"};
                          public int Count { get { return mData.Length; } }
                          public object Data { get { return mData; } }
                          public object Names { get { return mNames.Cast<object>().ToArray(); } }
                      }
                  }
                  

                  这可以使用以下 vbscript 进行测试:

                  This can be tested using the following vbscript:

                  Option Explicit
                  Sub Main
                    Dim o, v
                    Set o = CreateObject("StackOverflow.MyArrayDemo")
                    WScript.Echo "Count " & o.Count & " type: " & TypeName(o.Data) & " names: " & TypeName(o.Names)
                    For Each v in o.Data : WScript.Echo CStr(v) : Next
                    For Each v in o.Names : WScript.Echo v : Next
                  End Sub
                  Main

                  您可以看到此处报告为 Variant() 的类型 - 即:变体数组.

                  You can see the type reported here as Variant() - ie: an array of variants.

                  C:Userspat>windowsSysWOW64cscript.exe -nologo arraytest.vbs
                  Count 10 type: Variant() names: Variant()
                  0
                  1
                  1
                  2
                  3
                  5
                  8
                  13
                  21
                  34
                  one
                  two
                  three
                  four
                  five

                  这篇关于如何正确编组 VB 脚本数组进出用 C# 编写的 COM 组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:跨不同域和不同应用程序(经典 ASP 和 ASP.NET)共享 cookie 下一篇:在经典 ASP 和 ASP.Net 之间共享登录系统

                  相关文章

                    • <bdo id='huQOF'></bdo><ul id='huQOF'></ul>
                    <i id='huQOF'><tr id='huQOF'><dt id='huQOF'><q id='huQOF'><span id='huQOF'><b id='huQOF'><form id='huQOF'><ins id='huQOF'></ins><ul id='huQOF'></ul><sub id='huQOF'></sub></form><legend id='huQOF'></legend><bdo id='huQOF'><pre id='huQOF'><center id='huQOF'></center></pre></bdo></b><th id='huQOF'></th></span></q></dt></tr></i><div id='huQOF'><tfoot id='huQOF'></tfoot><dl id='huQOF'><fieldset id='huQOF'></fieldset></dl></div>
                    <legend id='huQOF'><style id='huQOF'><dir id='huQOF'><q id='huQOF'></q></dir></style></legend>

                  1. <tfoot id='huQOF'></tfoot>
                  2. <small id='huQOF'></small><noframes id='huQOF'>