我得到了以下类,并希望从同一个类创建一个c#-API和COM-API。因为我不能创建列表,所以我想在COM-API中创建一个数组。
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace myapi
{
[AttributeUsage(AttributeTargets.All),
ClassInterface(ClassInterfaceType.AutoDispatch),
Guid("AEBE2021-209A-4AEE-9A42-04CB82BEC1CF")]
class DataProvider: System.Attribute
{
int[] data = { 1, 2, 3 };
[ComVisibleAttribute(false)]
public List<int> getDataAPI() { //Only Method which should be available for C#-API
return new List<int>(data);
}
[ComVisibleAttribute(true)]
public int[] getDataCom() //Only Method which should be available for COM
{
return data;
}
}
}我知道我能够用COM-API的ComVisibleAttribute隐藏第一个方法。有没有办法隐藏或限制c#-API的第二个方法的访问?(我指的是正常的构建)
对于用户界面,最好的解决方案是为两种方法提供相同的名称。
还是我错过了什么?有没有更好的方法将列表带到Com-Interface。
发布于 2015-05-12 16:20:27
这取决于COM客户端。例如,如果是jscript,那么无论如何都不能使用数组。在这种情况下,您必须自己提供所有的收集方法,如GetItem(索引)等。
例如,如果客户端是VB,您应该能够使用旧的ArrayList而不是数组,或者实现自定义的集合类。
您不能阻止.NET客户端调用您的公共方法,但您可以使用EditorBrowsableAttribute,如下所示:
[EditorBrowsable(EditorBrowsableState.Never)]
public int[] getDataCom()在这种情况下,该属性将从自动完成中隐藏(除非从同一程序集中使用)。总比什么都没有好。
https://stackoverflow.com/questions/30184286
复制相似问题