473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Typecasting between ref class and interface class

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

Jun 23 '07 #1
5 2091
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.

--
SvenC
Jun 23 '07 #2
On Jun 23, 4:50 pm, "SvenC" <S...@community.nospamwrote:
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.

--
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

Jun 23 '07 #3
>>IFileType ^ft = reinterpret_cast<IFileType ^>
>>Activator::CreateInstance(Type::GetType("Workspa ce.FileType,
TextFile"));

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.
How is TextFile defined?

--
SvenC
Jun 24 '07 #4
ew******@gmail.com wrote:
On Jun 23, 4:50 pm, "SvenC" <S...@community.nospamwrote:
>Hi,
>>namespace Workspace
{
public interface class IFileType {
public:
virtual String ^GetFileType();
};
}
>>IFileType ^ft = reinterpret_cast<IFileType ^>
Activator::CreateInstance(Type::GetType("Workspa ce.FileType,
TextFile"));

Try dynamic_cast instead.

--
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.
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
Jun 24 '07 #5
On Jun 24, 9:50 am, "Carl Daniel [VC++ MVP]"
<cpdaniel_remove_this_and_nos...@mvps.org.nospamwr ote:
ewpat...@gmail.com wrote:
On Jun 23, 4:50 pm, "SvenC" <S...@community.nospamwrote:
Hi,
>namespace Workspace
{
public interface class IFileType {
public:
virtual String ^GetFileType();
};
}
>IFileType ^ft = reinterpret_cast<IFileType ^>
Activator::CreateInstance(Type::GetType("Workspac e.FileType,
TextFile"));
Try dynamic_cast instead.
--
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.

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

Jun 24 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kapil Khosla | last post by:
Hi, I have been trying to understand this concept for quite sometime now somehow I am missing some vital point. I am new to Object Oriented Programming so maybe thats the reason. I want to...
7
by: Nicolay Korslund | last post by:
Hi! I'm having a little trouble with the typecast operator, can anybody help me with the rules for when the this operator is invoked? In the class 'Test' in the code below, the typecast operator...
3
by: cman | last post by:
#include <stdio.h> class Base { public: Base() { printf("Base()\n"); } ~Base()
2
by: Arun Prasath | last post by:
Hi all, I have the following question regd pointer typecasting. Is the following type of pointer typecasting valid? #define ALLOC(type,num) ((type *)malloc(sizeof(type)*num)) /*begin...
11
by: Vinod Patel | last post by:
I have a piece of code : - void *data; ...... /* data initialized */ ...... struct known_struct *var = (struct known_struct*) data; /*typecasting*/ How is this different from simple...
3
by: jdm | last post by:
In the sample code for the SortedList class, I see the use of a string typecasting macro consisting of a single letter "S". i.e.: Sortedlist->Add(S"Keyval one", S"Item one"); Now I have...
12
by: bwaichu | last post by:
What is the best way to handle this warning: warning: cast from pointer to integer of different size I am casting in and out of a function that requires a pointer type. I am casting an...
12
by: chadsspameateremail | last post by:
What I am trying to do is implement polymorphism in C. Why? This is to build a library which will be a C library and callable from C. However, I want to have polymorphic functions which are...
3
by: ewpatton | last post by:
Hi, I have written a DLL in C++.NET that contains a class inherited from a single abstraction: //------ IThing.h ------ #ifndef __ITHING_H__ #define __ITHING_H__ namespace DLLObject
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.