Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / June 2007

Tip: Looking for answers? Try searching our database.

Typecasting between ref class and interface class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ewpatton@gmail.com - 23 Jun 2007 21:30 GMT
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
SvenC - 23 Jun 2007 21:50 GMT
Hi,

> namespace Workspace
> {
[quoted text clipped - 7 lines]
> Activator::CreateInstance(Type::GetType("Workspace.FileType,
> TextFile"));

Try dynamic_cast instead.

--
SvenC
ewpatton@gmail.com - 23 Jun 2007 21:55 GMT
> Hi,
>
[quoted text clipped - 14 lines]
> --
> SvenC

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
SvenC - 24 Jun 2007 10:29 GMT
>>> IFileType ^ft = reinterpret_cast<IFileType ^>
>>> Activator::CreateInstance(Type::GetType("Workspace.FileType,
[quoted text clipped - 6 lines]
> Workspace.IFileType. That confuses me since FileType inherits from
> IFileType.

How is TextFile defined?

--
SvenC
Carl Daniel [VC++ MVP] - 24 Jun 2007 14:50 GMT
>> Hi,
>>
[quoted text clipped - 19 lines]
> 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
ewpatton@gmail.com - 24 Jun 2007 17:34 GMT
On Jun 24, 9:50 am, "Carl Daniel [VC++ MVP]"
<cpdaniel_remove_this_and_nos...@mvps.org.nospam> wrote:
> ewpat...@gmail.com wrote:
> >> Hi,
[quoted text clipped - 30 lines]
>
> -cd

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

Evan

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.