Is there a nice way to find out the Interop Object Type?

Pioneer
Pioneer

Hey there,

 

I'm trying to find a bitmap inside a draft. Problem is, there are several drawing objects inside the draft and there is no way to know the type in advance of the cast, at least I haven't found one. So what I'm doing now is pretty ugly, I'm trying to cast every drawing object and if it fails, I try the next one:

 

for (int j = 1; j <= drawingObjects.Count; j++)
{
    try
    {
        bitmap = (SmartFrame2d)drawingObjects.Item(j);
    }
    catch (System.Exception)
    {
        continue;
    }

 

    ...

}

 

Is there a nicer way to do this?

 

Jonas

6 REPLIES

Re: Is there a nice way to find out the Interop Object Type?

Siemens Phenom Siemens Phenom
Siemens Phenom

Hi Jonas,

 

The best way I've seen mentioned is to use the as keyword, especially if you are going to need to cast the object and use it as an object of that type. I think that would work in your case. For your example, the new code would be:

 

for (int j = 1; j <= drawingObjects.Count; j++)
{
    SmartFrame2d bitmap = drawingObjects.Item(j) as SmartFrame2d;

 

    if (bitmap != null)
    {
        ...

    }
}

 

Hope this helps,

Greg

Re: Is there a nice way to find out the Interop Object Type?

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

The correct way to identify the object type is to use Reflection. Once you have identified your the type, you can cast it and access its members:

SolidEdgeDraft.DraftDocument pDoc = (SolidEdgeDraft.DraftDocument)pSEApp.ActiveDocument;
SolidEdgeFrameworkSupport.DrawingObjects pDrawObjs = pDoc.ActiveSheet.DrawingObjects;
for (int i=1; i<=pDrawObjs.Count; i++)
{
  object pObj = pDrawObjs.Item(i);
  ObjectType objType = (ObjectType) pObj.GetType().InvokeMember("Type", BindingFlags.GetProperty, null, pObj, null);
  if (objType == ObjectType.igBalloon)
  {
    Balloon pBall = (Balloon)pObj;
    System.Diagnostics.Debug.WriteLine("Balloon found:  " + pBall.BalloonText);
  }
  else
  {
    System.Diagnostics.Debug.WriteLine("Object:  " + objType.ToString());
  }
}

 

 

Re: Is there a nice way to find out the Interop Object Type?

Siemens Phenom Siemens Phenom
Siemens Phenom

That is certainly another way to do it, but I'd think we'd agree that using the as keyword is much simpler and cleaner, especially since it needs to be casted and used anyway.

Re: Is there a nice way to find out the Interop Object Type?

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

Greg, you are right!!

I did not know about this operator in C#. It looks similar to the COM smart pointers in C++, which behave the same and return a NULL pointer, if the variable is of another type.

Always learning from these groups ;-)

Thanks.

Re: Is there a nice way to find out the Interop Object Type?

Siemens Phenom Siemens Phenom
Siemens Phenom

No problem - I just learned about it myself  Smiley Happy

Re: Is there a nice way to find out the Interop Object Type?

Pioneer
Pioneer

Thank you, this is indeed much cleaner. I too didn't know the "as" keyword, it will come very handy in my future apps.

 

Jonas