使用 int[] 参数从 testcomplete 调用 c# dll 函数
Call a c# dll function from testcomplete with a int[] parameter
我正在尝试从 testcomplete 代码中调用 c# 库函数。
我根据以下 Helen 的回答制作的 c# 库的结构如下:(库的构建目标是 3.5,具有通用的强名称密钥和 32/64 位的 regasm)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace TestCompleteTest
{
[Guid("122FD1AB-839F-4A3A-8A08-973A3836216B")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
[ProgId("ArrayTest")]
public class ArrayTest: IArrayTest
{
public void ArrayFunction(int[] arrayParameter)
{
using (StreamWriter writer = File.AppendText("C:\arraylog.txt"))
{
writer.Write("\r\nLog Entry : ");
writer.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString());
writer.WriteLine(" :");
writer.WriteLine(" :{0}", arrayParameter.Count());
writer.WriteLine("-------------------------------");
}
}
}
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("00655BDE-A768-4EC0-9C9A-A903976195F2")]
[ComVisible(true)]
public interface IArrayTest
{
void ArrayFunction(int[] arrayParameter);
}
}
我正在尝试从 TestComplete 10.40 桌面客户端中调用此函数,如下所示:
procedure ArrayTest;
var arrayTest, arrType, arrayOfIntegers;
begin
arrayTest := Sys.OleObject('ArrayTest');
//Create array type
arrType := dotNET.System.Type.GetType('System.Int32');
//Create array
arrayOfIntegers := dotNET.System.Array.CreateInstance(arrType, 5);
//Fill array
arrayOfIntegers.SetValue(1, 0);
arrayOfIntegers.SetValue(2, 0);
arrayOfIntegers.SetValue(3, 0);
arrayOfIntegers.SetValue(4, 0);
arrayOfIntegers.SetValue(5, 0);
arrayTest.ArrayFunction(arrayOfIntegers);
end;
这仍然会导致异常,并在 testcomplete 函数的以下行中显示消息 "The parameter is incorrect"。:
arrayTest.ArrayFunction(arrayOfIntegers);
非常感谢任何进一步的帮助
TestComplete DelphiScript 是一种基于 ActiveScript 的语言(类似于 Windows Script Host 的 VBScript 和 JScript)并使用 Variant 数据类型。虽然像数字、布尔值和字符串这样的简单值被正确编组到 .NET 中,但 "complex" 像数组和枚举这样的值需要在 .NET 端使用 dotNET
包装器创建:
...
var arrayOfIntegers, arrType, enumType, enumParameter;
begin
// Create a .NET array, e.g. {5}
arrType := dotNET.System.Type.GetType('System.Int32');
arrayOfIntegers := dotNET.System.Array.CreateInstance(arrType, 1);
arrayOfIntegers.SetValue(5, 0);
// Create an enumeration value, e.g. DayOfWeek.Tuesday
enumType := dotNET.System.Type.GetType('System.DayOfWeek');
enumParameter := dotNET.System.Enum.Parse(enumType, 'Tuesday');
functionResult := CSharpLibrary.CSharpFunction(arrayOfIntegers, enumParameter);
end;
UPD: 无需使 .NET 程序集 COM 可见即可在 TestComplete 中使用它们。您需要将程序集添加到项目属性 > CLR Bridge。然后你可以这样调用函数:
arrayTest := dotNET.TestCompleteTest.ArrayTest.zctor();
arrayTest.ArrayFunction(arrayOfIntegers);
或者如果 ArrayFunction
是静态方法:
dotNET.TestCompleteTest.ArrayTest.ArrayFunction(arrayOfIntegers);
文档有更多详细信息:Calling Functions From .NET Assemblies。
另外,在SetValue
方法中,第2个参数是元素索引,所以应该是:
arrayOfIntegers.SetValue(1, 0); // value, index
arrayOfIntegers.SetValue(2, 1);
arrayOfIntegers.SetValue(3, 2);
arrayOfIntegers.SetValue(4, 3);
arrayOfIntegers.SetValue(5, 4);
我正在尝试从 testcomplete 代码中调用 c# 库函数。
我根据以下 Helen 的回答制作的 c# 库的结构如下:(库的构建目标是 3.5,具有通用的强名称密钥和 32/64 位的 regasm)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace TestCompleteTest
{
[Guid("122FD1AB-839F-4A3A-8A08-973A3836216B")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
[ProgId("ArrayTest")]
public class ArrayTest: IArrayTest
{
public void ArrayFunction(int[] arrayParameter)
{
using (StreamWriter writer = File.AppendText("C:\arraylog.txt"))
{
writer.Write("\r\nLog Entry : ");
writer.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString());
writer.WriteLine(" :");
writer.WriteLine(" :{0}", arrayParameter.Count());
writer.WriteLine("-------------------------------");
}
}
}
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("00655BDE-A768-4EC0-9C9A-A903976195F2")]
[ComVisible(true)]
public interface IArrayTest
{
void ArrayFunction(int[] arrayParameter);
}
}
我正在尝试从 TestComplete 10.40 桌面客户端中调用此函数,如下所示:
procedure ArrayTest;
var arrayTest, arrType, arrayOfIntegers;
begin
arrayTest := Sys.OleObject('ArrayTest');
//Create array type
arrType := dotNET.System.Type.GetType('System.Int32');
//Create array
arrayOfIntegers := dotNET.System.Array.CreateInstance(arrType, 5);
//Fill array
arrayOfIntegers.SetValue(1, 0);
arrayOfIntegers.SetValue(2, 0);
arrayOfIntegers.SetValue(3, 0);
arrayOfIntegers.SetValue(4, 0);
arrayOfIntegers.SetValue(5, 0);
arrayTest.ArrayFunction(arrayOfIntegers);
end;
这仍然会导致异常,并在 testcomplete 函数的以下行中显示消息 "The parameter is incorrect"。: arrayTest.ArrayFunction(arrayOfIntegers);
非常感谢任何进一步的帮助
TestComplete DelphiScript 是一种基于 ActiveScript 的语言(类似于 Windows Script Host 的 VBScript 和 JScript)并使用 Variant 数据类型。虽然像数字、布尔值和字符串这样的简单值被正确编组到 .NET 中,但 "complex" 像数组和枚举这样的值需要在 .NET 端使用 dotNET
包装器创建:
...
var arrayOfIntegers, arrType, enumType, enumParameter;
begin
// Create a .NET array, e.g. {5}
arrType := dotNET.System.Type.GetType('System.Int32');
arrayOfIntegers := dotNET.System.Array.CreateInstance(arrType, 1);
arrayOfIntegers.SetValue(5, 0);
// Create an enumeration value, e.g. DayOfWeek.Tuesday
enumType := dotNET.System.Type.GetType('System.DayOfWeek');
enumParameter := dotNET.System.Enum.Parse(enumType, 'Tuesday');
functionResult := CSharpLibrary.CSharpFunction(arrayOfIntegers, enumParameter);
end;
UPD: 无需使 .NET 程序集 COM 可见即可在 TestComplete 中使用它们。您需要将程序集添加到项目属性 > CLR Bridge。然后你可以这样调用函数:
arrayTest := dotNET.TestCompleteTest.ArrayTest.zctor();
arrayTest.ArrayFunction(arrayOfIntegers);
或者如果 ArrayFunction
是静态方法:
dotNET.TestCompleteTest.ArrayTest.ArrayFunction(arrayOfIntegers);
文档有更多详细信息:Calling Functions From .NET Assemblies。
另外,在SetValue
方法中,第2个参数是元素索引,所以应该是:
arrayOfIntegers.SetValue(1, 0); // value, index
arrayOfIntegers.SetValue(2, 1);
arrayOfIntegers.SetValue(3, 2);
arrayOfIntegers.SetValue(4, 3);
arrayOfIntegers.SetValue(5, 4);