开发者

How can I write a native templated backing storage for a generic interface?

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. H开发者_如何学JAVAow 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.


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.


This builds fine in Visual Studio 2010:

template<class T>
public interface class ICustomCollection
{
     virtual void Add(T item);
     virtual bool Remove(T item);
     virtual bool Contains(T item);
};

template<class T>
public ref class GenericCustomCollection : ICustomCollection<T>
{
    virtual void Add(T item){ }
    virtual bool Remove(T item){ return false; }
    virtual bool Contains(T item){ return false; }
};

public ref class ConcreteCustomCollection : ICustomCollection<int>
{
public:
    virtual void Add(int item){ }
    virtual bool Remove(int item){ return false; }
    virtual bool Contains(int item){ return false; }
};

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜