如何为通用接口编写本机模板化后备存储?
比如说,我有一个界面;
public interface ICustomCollection<T>
{
void Add(T item);
bool Remove(T item);
bool Contains(T item);
}
我想创建一个访问本机 C/C++ dll(我也会创建)的类,该类提供了实现。我该如何编写托管类和本机代码才能使其工作?我了解互操作基础知识,但我不知道如何在这种情况下处理泛型类型。
I have an interface, say;
public interface ICustomCollection<T>
{
void Add(T item);
bool Remove(T item);
bool Contains(T item);
}
I would like to create a class that access native C/C++ dll (which I'd also create) which provide the implementation. How can I go about writing the managed class and native code for this to work? I know interop basics but I don't know how to handle generic types in this context.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当你的想法稍微转变一下,要求 T 是一个从 Object 派生的接口类型(你也应该这么做)时,C++ 接口就变得更加明显了。
When you make a slight shift in your thinking and require that T be an interface type that derives from Object, and you should, then the C++ interface becomes a lot more obvious.
这在 Visual Studio 2010 中构建得很好:
我只是制作了构建所需的最低限度,您可以使用它作为实现的起点。
如果您正在开始使用 C++/CLI,这是一本好书:Expert C++/命令行界面
This builds fine in Visual Studio 2010:
I just made the minimum needed for it to build, you can use this as a starting point for your implementation.
If you're getting started with C++/CLI, this is a good book: Expert C++/CLI