装载和/ JScript中传递数组到C#(不是C)COM组件 [英] Loading and Passing JScript Arrays from/to C# (not C) COM Component
问题描述
我已经看过教程上的JScript数组,但是没有看到它。我看到了类似的东西问,但涉及的Win32 code不是.NET。
I have looked at tutorials on jscript arrays, but not seeing it yet. I saw something similar asked but involving Win32 code not .NET.
想知道,我怎么传递数组来回JScript和一个C#COM组件之间?
Wondering, how do I pass arrays back and forth between JScript and a C# COM component?
例如,我在我的C#COM,返回字符串和整型数组定义的两个属性:
For example, I have two properties defined in my C# COM that returns string and integer arrays as:
string[] HeaderLines { get; set; }
int[] ImagePixels { get; set; }
在我的test.js文件:
In my test.js file:
...
var T = new ActiveXObject("Imager.Reader");
...
var headerLines = T.HeaderLines;
WScript.StdOut.WriteLine("First HeaderLine:" + headerLines[0]);
var pixels = T.ImagePixels;
WScript.StdOut.WriteLine("First Pixel: " + pixels[0]);
错误就是headerLines [0]写出:C:\\ TEMP \\ test.js(12:1)微软JScript运行时错误:'headerLines.0'为空或不是对象
The error is where headerLines[0] is written out: C:\temp\test.js(12, 1) Microsoft JScript runtime error: 'headerLines.0' is null or not an object
如果我删除headerLines在test.js,然后我得到这个错误(本质上是相同的,但对于整型数组):C:\\ TEMP \\ test.js(12:1)微软JScript运行时错误:'像素。 0'为空或不是对象
If I remove the headerLines in test.js, then I get this error (essentially the same but for the integer array): C:\temp\test.js(12, 1) Microsoft JScript runtime error: 'pixels.0' is null or not an object
我可以从非数组属性效果蛮好的,以及将值传递给其它方法,但不能与数组。我还需要在字符串和整型数组传递到我的C#COM组件定义为一个方法:
I can get results from non-array properties just fine as well as passing values to other methods, but not with arrays. I also need to pass in string and integer arrays into a method defined in my C# COM component as:
bool Write(string filename, string[] headerLines, int[] pix, int width, int height);
我使用Visual Studio的前preSS 2012桌面创建在C#中的COM件。如果需要任何其他信息,只是让我知道。
I am using Visual Studio Express 2012 for Desktop for creating the COM piece in C#. If any other information is needed, just let me know.
推荐答案
微软JScript引擎执行JavaScript数组为 IDispatchEx
的对象。从C#,他们可以通过反射或动态
对象进行操作(与动态
,它只能存取性能和像长度
,推()
等方法,但不引用其实际指数元素)。例如:
Microsoft JScript engine implements JavaScript arrays as IDispatchEx
objects. From C#, they can be manipulated via reflection or as dynamic
objects (with dynamic
, it's only possible to access properties and methods like length
, push()
etc., but not reference actual elements by their indices). Example:
JavaScript的:
var T = new ActiveXObject("MySimulator.World");
var ar = ["a", "b", "c"];
T.MyFunction(ar);
C#:
public void MyFunction(object array)
{
dynamic dynArray = array;
int length = dynArray.length;
dynArray.push("d");
SetAt(array, 1, "bb");
Console.WriteLine("MyFunction called, array.length: " + length);
Console.WriteLine("array[0]: " + GetAt(array, 0));
Console.WriteLine("array[1]: " + GetAt(array, 1));
Console.WriteLine("array[3]: " + GetAt(array, 3));
}
static object GetAt(object array, int index)
{
return array.GetType().InvokeMember(index.ToString(),
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty,
null, array, new object[] { });
}
static object SetAt(object array, int index, object value)
{
return array.GetType().InvokeMember(index.ToString(),
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.SetProperty,
null, array, new object[] { value });
}
输出:
MyFunction called, array.length: 3
array[0]: a
array[1]: bb
array[3]: d
这篇关于装载和/ JScript中传递数组到C#(不是C)COM组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!