dynamic runtime casting?

  • Thread starter Thread starter Larry Minton
  • Start date Start date
L

Larry Minton

Is there a C++ method comparable to the vb.net TryCast function? I
had hopes for Convert::ChangeType, but that didn't work.

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

Larry
 
to use dynamic_cast requires specifying at compile time the class to
cast to. We don't have that, we are determining the class we want to
cast to at runtime.

return intern(dynamic_cast<l_pInterface^>(l_pObject));

--> error C2061: syntax error : identifier 'l_pInterface'

Larry

look up "dynamic_cast"


Larry Minton said:
Is there a C++ method comparable to the vb.net TryCast function? I
had hopes for Convert::ChangeType, but that didn't work.

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

Larry
 
Larry said:
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
 
If the type to cast to is only known at runtime you can use:

return l_pType.IsInstanceOfType(l_pObject) ? l_pObject : nullptr;

Is there a C++ method comparable to the vb.net TryCast function? I
had hopes for Convert::ChangeType, but that didn't work.

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));
 
Ah, ok, thanks. I was thinking of it as returning a "different" object
(i.e., running GetType() on the object would return the interface
type) when it is always still just the same object. I'll need to
adjust my thinking...

Thanx, Larry

If the type to cast to is only known at runtime you can use:

return l_pType.IsInstanceOfType(l_pObject) ? l_pObject : nullptr;

Is there a C++ method comparable to the vb.net TryCast function? I
had hopes for Convert::ChangeType, but that didn't work.

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));
 
Carl,

The intern method actually wraps the object in another object (a
DotNetObject), and that outer object is what is used by the scripting
engine in an object type it understands (a dotNetObject). This is for
a C++ application with a typeless scripting language, we are adding
..net access to the scripting language. So the user can call inspection
functions on the dotNetObject, and via reflection we expose the
methods/properties/events/etc of the wrapped .net object.

We already have the concept of interfaces in the scripting language,
where you can get interfaces that are exposed by our app's objects and
operate on those interfaces. I'm trying to extend that concept to the
..net exposure. I'm thinking that I'll need to tweak the DotNetObject
so that I optionally specify the Type to expose it as, rather than
just take the Type from the object. So then we would have:

myString = dotNetObject "System.String" "AAA"
showMethods myString -- shows all methods of String
newString = myString.Copy()

iclonable = getInterface myString "IClonable"
showMethods iclonable -- shows just that methods of IClonable
interface
clonedString = iclonable.Clone()

Our current "interfaces" are actually objects, which colors my
thinking here.

Larry

Larry said:
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
 
Back
Top