05-18-2023
10:56 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
05-18-2023
10:56 AM
This is the way I access the Type property for the com objects using reflection so I don't have to embed types or add more dependencies.
Type objType = obj.GetType();
ObjectTypeEnum ote;
try
{
ote = (ObjectTypeEnum)objType.InvokeMember("Type", BindingFlags.InvokeMethod, null, obj, null);
}
catch (Exception castExp)
{
throw new InvalidCastException("Object is not an Inventor Type:/n" + castExp.Message);
}
I also took this one step further and passed the ote variable to a giant switch statement to return the actual Inventor type. You can also have it only contain the types you're interested in.
public Type GetInventorType(object obj)
{
Type rVal = null;
if (obj != null)
{
Type objType = obj.GetType();
ObjectTypeEnum ote;
try
{
ote = (ObjectTypeEnum)objType.InvokeMember("Type", BindingFlags.InvokeMethod, null, obj, null);
}
catch (Exception castExp)
{
throw new InvalidCastException("Object is not an Inventor Type:/n" + castExp.Message);
}
switch (ote)
{
case ObjectTypeEnum.kAngularGeneralDimensionObject:
rVal = typeof(AngularGeneralDimension);
break;
case ObjectTypeEnum.kApplicationAddInObject:
rVal = typeof(ApplicationAddIn);
break;
case ObjectTypeEnum.kApplicationAddInSiteObject:
rVal = typeof(ApplicationAddInSite);
break;
case ObjectTypeEnum.kApplicationAddInsObject:
rVal = typeof(ApplicationAddIns);
break;
case ObjectTypeEnum.kApplicationEventsObject:
rVal = typeof(ApplicationEvents);
break;
case ObjectTypeEnum.kApplicationObject:
rVal = typeof(Application);
break;
case ObjectTypeEnum.kDiameterGeneralDimensionObject:
rVal = typeof(DiameterGeneralDimension);
break;
default:
break;
}
}
return rVal;
}