[英]how to expose a c# class as a com objects interface?
I have ac# class P2PLib. 我有AC#类P2PLib。
public int portRecv=10101;
public int portSend = 10102;
public int multicastPort=10103;
int memberNum = 0;
string data;
string time;
List<Member> MemberList = new List<Member>();
public void DisplayMembers();
public void start(...);
public void join(..);
public void leave(..);
void add(...);
void remove(...);
How do I create server side code of this class in my interop communication between c# and c++? 如何在c#和c ++之间的互操作通信中创建此类的服务器端代码?
http://msdn.microsoft.com/en-us/library/aa645738(v=vs.71).aspx example shows how how we can write interfaces which can be groups of methods http://msdn.microsoft.com/zh-cn/library/aa645738(v=vs.71).aspx示例演示了如何编写可作为方法组的接口
but I am confused as to how will my variables like portsend and others will be initiated in c++ client side code. 但是我对于如何在c ++客户端代码中启动像portend这样的变量和其他变量感到困惑。
---edit---can I keep persistent data with com interfaces? ---编辑---我可以使用com接口保留持久数据吗? for eg the list mentioned above? 例如上面提到的清单? Will I be able to create an object of this class in the unmanaged code communicating with the managed codes com object? 我可以在与托管代码com对象通信的非托管代码中创建此类的对象吗?
You cannot expose fields in a COM interface, only property and methods are supported. 您不能在COM界面中公开字段,仅支持属性和方法。 This is in general a good and widely adopted practice in C# programming, helps here as well: 通常,这是C#编程中的一种良好且被广泛采用的做法,在这里也有帮助:
public class PortWrapper {
public int ReceivePort {
get { return portRecv; }
set {
if (value == portRecv) return;
if (value < 256 || value > 65535) throw new ArgumentOutOfRangeException();
portRecv = value;
setupReceiver();
}
}
// etc..
private int portRecv=10101;
}
Fall into this pit of success by actually declaring an interface in your C# code. 通过在您的C#代码中声明一个接口来落入成功的陷阱。 An all-around good idea since that lets you hide the implementation class details with [ClassInterface(ClassInterfaceType.None)] and expose the pure interface with [InterfaceType(ComInterfaceType.InterfaceIsDual)]. 一个全面的好主意,因为它使您可以使用[ClassInterface(ClassInterfaceType.None)]隐藏实现类的详细信息,并使用[InterfaceType(ComInterfaceType.InterfaceIsDual)]公开纯接口。 That's the natural COM way. 那是自然的COM方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.