How to use C++ ATL-Template class in c#
-
I am migrating a C++ application to .NET. In C++ ,I have an template class which uses some ATL library classes for eg. template< class T > class MyBaseClass: public IConnectionPointContainerImpl, public IPersistStorageImpl, public IPersistPropertyBagImpl, { } Now i need to use this call in my new C# .net project where my new .NET call will get derive from above MyBaseClass . Please let me know how can i archive this ??? Is this possible to use a template class which uses some ATL libraray classes directly in C# ? `chiman
-
I am migrating a C++ application to .NET. In C++ ,I have an template class which uses some ATL library classes for eg. template< class T > class MyBaseClass: public IConnectionPointContainerImpl, public IPersistStorageImpl, public IPersistPropertyBagImpl, { } Now i need to use this call in my new C# .net project where my new .NET call will get derive from above MyBaseClass . Please let me know how can i archive this ??? Is this possible to use a template class which uses some ATL libraray classes directly in C# ? `chiman
There is no straightforward method to use template classes in other CLI languages like C#. This is because of the difference in how C++ templates and CLI generics works. C++ templates will be instantiated at compile time and CLI generics will be on runtime. C++/CLI also supports managed templates, but unfortunately it is not portable to other CLI languages. If you want to use inheritance, you may need to create managed classes that calls the template class for each known type. Like
MyBaseClassInt
which useMyBaseClass<int>
etc. This will not be the correct method as you will have class explosion when you have several types to be used. Prefer composition over inheritance. In such case, this can be easily solved by providing a factory class which checks the type and instantiates respective template instance. Any new types can be easily added to the factory method without recompiling the whole application. We are taking the capability of C++/CLI to mix generic and template type parameters. To see how this can be done, consider the following code:// Your existing class - Keep it intact
template< class T >
class MyBaseClass:
public IConnectionPointContainerImpl,
public IPersistStorageImpl,
public IPersistPropertyBagImpl,{
public:
void DoSomething();
};// Define a managed generic interface that all CLI languages can use
// This interface should have all the methods that other languages need access
// Note that this uses generics which is supported by CLIgeneric<typename T>
public interface class IManagedBase
{
public:
void DoSomething();
};// This is implementation for the above interface
// This uses managed template class and work with mixing generic type and template typetemplate<typename T>
ref class MyBaseClassWrapper : IManagedBase<T>
{
public:
virtual void DoSomething()
{
// Using native class
MyBaseClass<T> native;
native.DoSomething();
}
};// Provide a simple builder class which takes care about instantiations and returns interface
// This will be used by other CLI languagespublic ref class NativeObjectBuilder
{
public:
// Add all the types you know here
generic<typename T> virtual IManagedBase<T> GetBaseObject()
{
// typeid gives the compile time type
if(T::typeid == int::typeid)
return (IManagedBase<T>^)