到目前为止,我在C#.NET 4中制作了一个组件,并使用System.EnterPriseservices使其可见。我想在C#中开发业务方法,但是我仍然需要从经典ASP(VBScript)访问它们。

到目前为止,一切都很好,一切正常(Exept功能过载:))。现在,我上了一个测试课程,以获得更多的回报代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.EnterpriseServices;
using System.Management;

namespace iController
{
  /// <summary>
  /// The tools class provides additional functions for general use in out of context to other classes of the iController.
  /// </summary>
  public class tools :ServicedComponent
  {

    #region publich methods

    public bool TestBoolean()
    {
      return true;
    }

    public string TestString()
    {
      return "this is a string";
    }

    public int TestInteger()
    {
      return 32;
    }

    public double TestDouble()
    {
      return 32.32;
    }

    public float TestFloat()
    {
      float ret = 2 ^ 16;
      return ret;
    }

    public string[] TestArray()
    {
      string[] ret = {"0","1"};
      return ret;
    }

    public int[][] TestJaggedArray()
    {
      int[][] jaggedArray = new int[3][];
      jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };
      jaggedArray[1] = new int[] { 0, 2, 4, 6 };
      jaggedArray[2] = new int[] { 11, 22 };
      return jaggedArray;
    }

    public Dictionary<string, string> TestDictionary()
    {
      Dictionary<string, string> ret = new Dictionary<string,string>();
      ret.Add("test1","val1");
      ret.Add("test2","val2");
      return ret;
    }

    #endregion

  }
}

然后,我只是制作了一个简单的vbscript文件,可以使用cscript.exe来测试porpuse。

Dim oTools : Set oTools = CreateObject("iController.tools")

WScript.StdOut.WriteLine TypeName(oTools.TestBoolean()) & " - " & oTools.TestBoolean()
WScript.StdOut.WriteLine TypeName(oTools.TestString()) & " - " & oTools.TestString()
WScript.StdOut.WriteLine TypeName(oTools.TestInteger()) & " - " & oTools.TestInteger()
WScript.StdOut.WriteLine TypeName(oTools.TestDouble()) & " - " & oTools.TestDouble()
WScript.StdOut.WriteLine TypeName(oTools.TestFloat()) & " - " & oTools.TestFloat()

test = oTools.TestArray()
WScript.StdOut.WriteLine TypeName(test)
WScript.StdOut.WriteLine UBound(test)

For i = 0 To UBound(test)
  WScript.StdOut.WriteLine test(i)
Next

For Each item IN test
  WScript.StdOut.WriteLine item
Next

test = oTools.TestJaggedArray()
WScript.StdOut.WriteLine TypeName(test)
For Each item IN test
  WScript.StdOut.WriteLine test & " - " & test.Item(item)
Next

test = oTools.TestDictionary()
WScript.StdOut.WriteLine TypeName(test)
For Each item IN test
  WScript.StdOut.WriteLine test & " - " & test.Item(item)
Next

什么工作正常:

string, int, foat, double

当涉及数组,jaggedarray或字典时,我会得到类型的不匹配。 vartype是字典的13个对象,例如,这似乎与脚本不同。

我整天检查了CodeProject.com和Stackoverflow,但没有找到任何提示在Stackoverflow上脱颖而出,有人提到有人需要创建IDISPATCH接口。

因此,有人遇到过同样的问题,可以帮助我或给我一些我可以继续的提示?

有帮助吗?

解决方案

  1. 课程:让.NET处理您的元帅:)

    public object[] Read()    {      var retVal = new object[] {1,2,3};      return retVal;    }
    

这是我可以从vbscript访问的数组。线索是必须是对象[]。

现在,我继续为锯齿状阵列和词典的解决方案工作...

其他提示

将数组传递给com:所有托管阵列类型都可以从托管代码传递给非管理的代码。根据托管类型和应用于其的属性,可以将数组作为安全数组或C风格数组访问

看这篇文章:http://msdn.microsoft.com/en-us/library/z6cfh6e6(v=vs.80).aspx

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top