Trying to check the type of some Elements if they are (ITextElement , MarkerElement)
, when i try .getType()
function it returns always "System._COM_object".
How i could get the type of an object if i don't know its type before ? because i could only detect the types that i pretend using (obj as Interface) != null
.
but for the types that i don't pretend i get always null
from (obj as Interface)
.
Any help please.
1 Answer 1
In ArcObjects, like other COM-based libraries, interface testing is the main way you determine the type (using that term loosely) of object you have:
if (element is ITextElement):
// Do something
else if (element is IMarkerElement):
// Do something else
etc.
Another thing you can do is, if the object implements IPersist
, you can get its CLSID using GetClassID
, and, from there (if desired), you can get its ProgID using the WinAPI ProgIDFromCLSID
function. This is useful for cases where the object has no other indicative interfaces.
Lastly, and I've never used this myself as it seems very unwieldy and potentially very slow, is to use reflection to query interface the object against every interface in an assembly to see which interfaces it supports:
IntPtr iunkwn = Marshal.GetIUnknownForObject(pWorkspace);
// enum all the types defined in the interop assembly
System.Reflection.Assembly objAssembly =
System.Reflection.Assembly.GetAssembly(typeof(IWorkspace));
Type[] objTypes = objAssembly.GetTypes();
// find the first implemented interop type
foreach (Type currType in objTypes)
{
// get the iid of the current type
Guid iid = currType.GUID;
if (!currType.IsInterface || iid == Guid.Empty)
{
// com interop type must be an interface with valid iid
continue;
}
// query supportability of current interface on object
IntPtr ipointer = IntPtr.Zero;
Marshal.QueryInterface(iunkwn, ref iid, out ipointer);
if (ipointer != IntPtr.Zero)
{
string str =currType.FullName;
}
}
(Source: Re: Getting the real COM class name)