Typecasting between ref class and interface class

E

ewpatton

I'm working on a modular application that handles working with
different file types. Each file type is defined in a separate DLL so
that new types can be defined and then imported. Here's the code I've
been using:

namespace Workspace
{
public interface class IFileType {
public:
virtual String ^GetFileType();
};

}

This interface is shared among all DLL files. In each DLL there is
supposed to be a definition like:

namespace Workspace
{
public ref class FileType : public IFileType {
public:
virtual String ^GetFileType() { return ".txt"; }
}

}

This keeps everything structured so the program just has to enumerate
through a list of DLLs looking for DLLs with the class
Workspace.FileType. The program then loads these DLLs dynamically
along the lines of:

IFileType ^ft = reinterpret_cast<IFileType ^>
Activator::CreateInstance(Type::GetType("Workspace.FileType,
TextFile"));

which loads the DLL and initiates an instance of the FileType class.

However, when I attempt to use the class:

ft->GetFileType();

I get a System.EntryPointNotFoundException exception. How do I
properly cast the System::Object ^ back to a copy of my IFileType ^
class?

Evan
 
S

SvenC

Hi,
namespace Workspace
{
public interface class IFileType {
public:
virtual String ^GetFileType();
};
}

IFileType ^ft = reinterpret_cast<IFileType ^>
Activator::CreateInstance(Type::GetType("Workspace.FileType,
TextFile"));

Try dynamic_cast instead.
 
E

ewpatton

Hi,



Try dynamic_cast instead.

I've tried both dynamic_cast<> and safe_cast<> and both throw
exceptions. They say they can't cast from Workspace.FileType to
Workspace.IFileType. That confuses me since FileType inherits from
IFileType.


Evan
 
S

SvenC

IFileType ^ft = reinterpret_cast said:
I've tried both dynamic_cast<> and safe_cast<> and both throw
exceptions. They say they can't cast from Workspace.FileType to
Workspace.IFileType. That confuses me since FileType inherits from
IFileType.

How is TextFile defined?
 
C

Carl Daniel [VC++ MVP]

I've tried both dynamic_cast<> and safe_cast<> and both throw
exceptions. They say they can't cast from Workspace.FileType to
Workspace.IFileType. That confuses me since FileType inherits from
IFileType.

Where is IFileType defined? To share the interface, best practice usually
involves putting all of your interface definitions in a DLL that's separate
from both the callers and the implementors. If you've simply repeated the
same interface definition in each assembly, then you actually have several
distinct types, all with the same name - but the CLR considers them to be
distinct types with no inheritance relationship, so you can't cast between
them.

-cd
 
E

ewpatton

Where is IFileType defined? To share the interface, best practice usually
involves putting all of your interface definitions in a DLL that's separate
from both the callers and the implementors. If you've simply repeated the
same interface definition in each assembly, then you actually have several
distinct types, all with the same name - but the CLR considers them to be
distinct types with no inheritance relationship, so you can't cast between
them.

-cd

Ah... well that explains it. Makes me miss the old days of just
casting it to a void * and back.

Evan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top