使用 ac# .net 的返回值将组件构建为 com+

发布于 2024-10-10 06:25:46 字数 3066 浏览 0 评论 0原文

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

到目前为止一切都很好(除了函数重载:))。 现在我做了一个测试类来获得更多返回代码的经验。

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

当涉及数组、锯齿状数组或字典时,我遇到类型不匹配的问题。 VarType 是字典的 13 个对象,例如,但这个字典似乎与 Scripting.Dictionary 不同。

我检查了 codeproject.com 和 stackoverflow 一整天,没有找到任何提示,除了 stackoverflow 上的一些线程,其中有人提到需要创建 IDispatch 接口。

那么有人遇到过同样的问题并且可以帮助我或给我一些可以继续的提示吗?

so far I made a component in C# .NET 4 and use System.EnterpriseServices to make it COM visible. I want to develop business methods in C#, but I still need to access them from classic ASP (vbscript).

So far so good, everything works fine (exept function overloading :)).
Now I made a test class to get more expirience with return code.

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

  }
}

Then I just made a simple vbscript file to run it with cscript.exe for testing 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

What works fine:

string, int, foat, double

When it comes to array, jaggedarray or dictionaries I get a type mismatch.
VarType is 13 object for the dictionary e.g. but this dict seems to be different then the Scripting.Dictionary.

I checked codeproject.com and stackoverflow all day and didn't find any hints exept some thread on stackoverflow where someone mentioned there is a need to created a IDispatch interface.

So anyone ever had the same issue and can help me or give me some hints I can go on with?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风追烟花雨 2024-10-17 06:25:46
  1. 教训:让 .NET 处理你的 MarshalAs :)

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

这是一个我可以从 vbscript 访问的数组。线索是它必须是 object[]。

现在我继续研究锯齿状数组和字典的解决方案......

  1. Lesson: let .NET handle your MarshalAs :)

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

This is a Array which I can access from vbscript. The clue is that is has to be object[].

Now I go on work on a solution for jagged arrays and dictionaries...

绮烟 2024-10-17 06:25:46

将数组传递给 COM:全部托管
数组类型可以传递给非托管类型
来自托管代码的代码。根据
托管类型和属性
应用到它,数组可以是
作为安全数组或 C 风格访问
数组

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

Passing Arrays to COM: All managed
array types can be passed to unmanaged
code from managed code. Depending on
the managed type and the attributes
applied to it, the array can be
accessed as a safe array or a C-style
array

Take a look at this article:
http://msdn.microsoft.com/en-us/library/z6cfh6e6(v=vs.80).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文