Re: dynamic runtime casting?
- From: "Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@xxxxxxxxxxxxxxx>
- Date: Thu, 18 May 2006 21:19:48 -0700
Larry Minton wrote:
Is there a C++ method comparable to the vb.net TryCast function? I
had hopes for Convert::ChangeType, but that didn't work.
dynamic_cast is identical to the VB TryCast function - they both compile to
the same IL.
For a scripting engine, the user is specifying to retrieve an
interface on an object by name. Code is roughly:
array<Type^> ^l_pInterfaces = l_pType->GetInterfaces();
for(int m=0; m < l_pInterfaces->Length; m++)
{
Type^ l_pInterface = l_pInterfaces[m];
System::String ^ l_pInterfaceName = l_pInterface->Name;
if (l_pPropString->Equals(l_pInterfaceName)
break;
l_pInterface = nullptr;
}
if (l_pInterface)
return intern(Convert::ChangeType(l_pObject,l_pInterface));
What exactly are you trying to return here? How do you plan to return an
object reference of unspecified type? AFIAK, the best you can do is just
plain 'object'. .NET is not like COM where there's a universal base
interface (IUnknown). All you have is a univeral object reference
(System::Object).
If you have an instance of System::Type and you want to know if a given
System::Object is reference convertible to that type, you can use
Type::IsInstanceOfType to determine if ths object is an instance of that
type.
HTH
-cd
.
- Follow-Ups:
- Re: dynamic runtime casting?
- From: Larry Minton
- Re: dynamic runtime casting?
- References:
- dynamic runtime casting?
- From: Larry Minton
- dynamic runtime casting?
- Prev by Date: Re: dynamic runtime casting?
- Next by Date: Re: dynamic runtime casting?
- Previous by thread: Re: dynamic runtime casting?
- Next by thread: Re: dynamic runtime casting?
- Index(es):
Relevant Pages
|