Get subObject from Object by name

Get subObject from Object by name

Hubert_Los
Advocate Advocate
215 Views
1 Reply
Message 1 of 2

Get subObject from Object by name

Hubert_Los
Advocate
Advocate

Hello,

I would like to get a subObject from Object by name using C#. Previously, I used the VBA function: "CallByName(obj, propertyName, VbGet)".

I can get primitive objects such as strings and booleans, but I have a problem with non-primitive objects. I tried using Reflection, but I don't know why it doesn't work. Has anyone tried, retrieve elements like this? Maybe the Inventor library has a function that does this. I think it does but I can't find it.

 

        public static void GetObjectProperties(object obj)
        {
            System.ComponentModel.PropertyDescriptorCollection oProps = System.ComponentModel.TypeDescriptor.GetProperties(obj);
            foreach (System.ComponentModel.PropertyDescriptor oProp in oProps)
            {

                object propertyValue = oProp.GetValue(obj);

                if(propertyValue != null)
                {
                    if (propertyValue.GetType().IsPrimitive || propertyValue.GetType() == typeof(string)) 
                    { 
                        
                    }
                    else
                    {

                        Type mainType = obj.GetType();
                        PropertyInfo subObjectProperty = mainType.GetProperty(oProp.Name);
                        object subObjectValue = subObjectProperty.GetValue(obj);

                    }
                }

            }
            return;
        }

 

 

 

0 Likes
216 Views
1 Reply
Reply (1)
Message 2 of 2

Michael.Navara
Advisor
Advisor

Use Reflection to call member by name is possible, but in my opinion it is the last chance how to Sava A Day.

 

Here is small example how to obtain ComponentDefinition or NULL from any document.

 

public void Main()
{
    var document = ThisDoc.Document;
    var componentDefinition = GetComponentDefinition(document);
    var surfaceBodies = componentDefinition.SurfaceBodies;
}

private ComponentDefinition GetComponentDefinition(Document document)
{
    //(document as PartDocument).ComponentDefinition
    //(document as AssemblyDocument).ComponentDefinition

    try
    {
        var componentDefinition = document.GetType().InvokeMember(
            "ComponentDefinition",
            BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty,
            null,
            document,
            null);

        return componentDefinition as ComponentDefinition;
    }
    catch
    {
        return null;
    }
}

 

 

0 Likes