469,646 Members | 1,208 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

dynamic runtime casting?

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
May 19 '06 #1
6 1672
look up "dynamic_cast"
"Larry Minton" <av*****@jadeinc.com> wrote in message
news:gq********************************@4ax.com...
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

May 19 '06 #2
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

On Fri, 19 May 2006 10:52:03 +0800, <www.fruitfruit.com> wrote:
look up "dynamic_cast"
"Larry Minton" <av*****@jadeinc.com> wrote in message
news:gq********************************@4ax.com.. .
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


May 19 '06 #3
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
May 19 '06 #4
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) );


--
Vladimir Nesterovsky
May 19 '06 #5
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

On Fri, 19 May 2006 10:02:35 +0200, "Vladimir Nesterovsky"
<vl******@nesterovsky-bros.com> wrote:
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) );


May 19 '06 #6
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

On Thu, 18 May 2006 21:19:48 -0700, "Carl Daniel [VC++ MVP]"
<cp*****************************@mvps.org.nospam > wrote:
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


May 19 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Jason Tesser | last post: by
5 posts views Thread by Thomas Hede Jensen | last post: by
5 posts views Thread by Radde | last post: by
3 posts views Thread by Stephen Gennard | last post: by
10 posts views Thread by Yechezkal Gutfreund | last post: by
2 posts views Thread by Martin Hart - Memory Soft, S.L. | last post: by
6 posts views Thread by Philipp Schumann | last post: by
6 posts views Thread by DaTurk | last post: by
13 posts views Thread by DaTurk | last post: by
3 posts views Thread by Trev | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.