我在C#中有一个COM对象和一个Silverlight应用程序(升级的权限),它是此COM对象的客户端。
COM对象:
[ComVisible(true)]
public interface IProxy
{
void Test(int[] integers);
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class Proxy : IProxy
{
[ComVisible(true)]
public void Test(int[] integers)
{
integers[0] = 999;
}
}
Silverlight客户端:
dynamic proxy = AutomationFactory.CreateObject("NevermindComProxy.Proxy");
int[] integers = new int[5];
proxy.Test(integers);
我检测到整数[0] == 999,但数组完好无损。
如何让COM对象修改数组?
UPD 适用于非silverlight应用程序。 Silverlight失败。如何修复silverlight?
答案 0 :(得分:1)
简短的回答是你需要通过ref传递数组(参见示例上方的AutomationFactory中的注释[数组通过C#中的值传递]) - 问题是,SL将使用调用proxy.Test(ref integers)
时的参数异常(我不是为什么)。解决方法是,如果方法通过ref获取对象,SL将通过ref传递数组,因此这可以...
[ComVisible(true)]
public interface IProxy
{
void Test( ref object integers);
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class Proxy : IProxy
{
[ComVisible(true)]
public void Test(ref object intObj)
{
var integers = (int[])intObj;
integers[0] = 999;
}
}
使用SL代码添加ref如:
dynamic proxy = AutomationFactory.CreateObject("NevermindComProxy.Proxy");
var integers = new int[5];
proxy.Test( ref integers);
从调用者或接口定义中删除引用,它不会更新数组。