How to get RuntimeTypeHandle of a Type?

  • Thread starter Thread starter mind_the_gap
  • Start date Start date
M

mind_the_gap

Hi,

I need to get the TypeHandle of Type since it is much more efficent to
store and to reference types.
The Type.GetTypeHandle method isn available but throws (like stated in
MSDN) a NotSupportedException. But the Type RuntimeTypeHandle is there
- have you any idea how to obtain a Type Handle in CF?

Thanks in advance,
Tom
 
If it is not implemented by the framework - which it is not, then there is no
way to do it.
 
hm, too bad.
But why is the Type.GetTypeHandle availabe and the the return type
(RuntimeTypeHandle) is also there? I thought not implemented methods
were just not there - instead, this one throws the
NotSupportedException.

But thanks anyway.
 
Type.GetTypeHandle is not available but static method Tpe.GetTypeFromHandle
is available which returns RuntimeTypeHandle (struct).
 
Well, thanks - unfortunately this function is the counterpart to the
GetHandle method since it returns the Type for a given Handle (as MSDN
says and the name implies) - do you mean another function?

But, if a function exists which could turn a RuntimeTypeHandle into a
Type - shouldn't it work somehow to get what I want, Type ->
Handle? :)

Thank you
 
I believe this line of code would work:
RuntimeTypeHandle myType = typeof(string).TypeHandle;
 
I believe this line of code would work:
RuntimeTypeHandle myType = typeof(string).TypeHandle;

Unfortunately, this would not work. The TypeHandle property is
available, yes, but if you use it you will get back a
NotSupportedException. I can´t understand why Microsoft would kick out
the RuntimeTypeHandle of a type - but even more strange is why there
is still the TypeHandle property (which throws the exception) and the
type RuntimeTypeHandle. At lease it gives me hope to find something
which will get me a TypeHandle of a type in compact framework.

So, if you have any hints, I would appreciate it :)

Thanks,
Tom
 
This does appear to be an oversight in the framework. All of the structures
and methods are there to support the RuntimeTypeHandle representation of a
type instance, but no way to get it.

For example, as mentioned, the Type class has a static method which accepts
a RuntimeTypeHandle instance, and this then invokes an exported method on
mscorlib.dll to get the type based on the handle's m_ptr member. Where is the
corresponding export to actually get the RuntimeTypeHandle instance from a
given Type instance?

As a workaround, can anyone confirm that the RuntimeType.pClassDesc field,
and the RuntimeTypeHandle.m_ptr field, both of which is are an IntPtr, are
the same pointer to the EEClass, which then Type.GetTypeFromHandle would
interpret correctly and give me a type instance, if a RuntimeType instance's
pClassDesc field were read via reflection or an unsafe mechanism and used to
construct a RuntimeTypeHandle?
 
Back
Top