Type.GetProperties() not working in custom inpesctor

I have the following class:

    public abstract class Binding : MonoBehaviour
    {
        public ViewModel ViewModel;
        public string ViewModelPropertyName;

        protected virtual void Awake()
        {
            VMManager.RegisterBinding(this);
        }

        public abstract void UpdatedBinding(object value);
    }

And his custom inspector:

[CustomEditor(typeof(Binding))]
    public class BindingEditor : Editor
    {
        //View Model
        private ViewModel[] viewModelList;
        private List<string> viewModelListStrings = new List<string>();
        private int viewModelListIndex;

        //Properties
        private List<string> viewModelPropertyListStrings = new List<string>();
        private int viewModelPropertyListIndex;

        private SerializedProperty viewModel;
        private SerializedProperty viewModelPropertyName;

        private void OnEnable()
        {
            viewModel = serializedObject.FindProperty("ViewModel");
            viewModelPropertyName = serializedObject.FindProperty("ViewModelPropertyName");

            viewModelList = (target as Binding).GetComponentsInParent<ViewModel>();

            int index = 0;
            viewModelListStrings.Clear();
            foreach (ViewModel vm in viewModelList)
            {
                if(vm == viewModel.objectReferenceValue)
                {
                    viewModelListIndex = index;
                }
                viewModelListStrings.Add($"{vm.GetType().Name}({vm.name})");
                index++;
            }

            viewModel.objectReferenceValue = viewModelList[viewModelListIndex];

            GetScriptProperties(viewModel.objectReferenceValue.GetType());
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            //View Model
            int lastviewModelIndex = viewModelListIndex;
            viewModelListIndex = EditorGUILayout.Popup("View Model", viewModelListIndex, viewModelListStrings.ToArray());

            if(lastviewModelIndex != viewModelListIndex)
            {
                viewModel.objectReferenceValue = viewModelList[viewModelListIndex];
                GetScriptProperties(viewModel.objectReferenceValue.GetType());
            }

            //Property
            int lastviewModelPropertyIndex = viewModelPropertyListIndex;
            viewModelPropertyListIndex = EditorGUILayout.Popup("Property", viewModelPropertyListIndex, viewModelPropertyListStrings.ToArray());

            if (lastviewModelPropertyIndex != viewModelPropertyListIndex)
            {
                viewModelPropertyName.stringValue = viewModelPropertyListStrings[viewModelPropertyListIndex];
            }

            serializedObject.ApplyModifiedProperties();
        }

        private void GetScriptProperties(System.Type viewModel)
        {
            PropertyInfo[] viewModelPropertyList = viewModel.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly);

            int index = 0;
            viewModelPropertyListStrings.Clear();
            foreach (PropertyInfo property in viewModelPropertyList)
            {
                if (viewModelPropertyName.stringValue == property.Name)
                {
                    viewModelPropertyListIndex = index;
                }
                viewModelPropertyListStrings.Add(property.Name);
                index++;
            }

            viewModelPropertyName.stringValue = viewModelPropertyListStrings[viewModelPropertyListIndex];
        }
    }

The line that is giving me problems is this:

PropertyInfo[] viewModelPropertyList = viewModel.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly);

This code works and the line above returns 1 property, so everything works as expected. But I need to show the custom inspector in child classes of Binding.cs, so I changed the [CustomEditor(typeof(Binding))] to [CustomEditor(typeof(Binding), true)] adding the editorForChildClasses as true.

Now the custom inspector works in child classes of Binding, but after this change, the GetProperties method is not working anymore, it always returns 0 properties. If I change the editorForChildClasses again to false, it works again.

You specify BindingFlags.DeclaredOnly in your GetProperties call, which instructs it to only return properties of the current type. When viewModel is a subclass of Binding, it will then only return properties directly declared on the subclass and not on the parent(s).

viewModel is not a subclass of Binding, it’s a separated class. I also tried to call GetProperties() without any parameter and still returning 0 properties.

viewModel is the following class:

public class TestVM : ViewModel
{
    public class Data : VMData
    {
        public string Symbol;
    }

    public string Symbol => data.Symbol;

    private Data data;
}

Did you check the Type object is the type you expect it to be? typeof(TestVM).GetProperties(); does return the Symbol property for me.

Ok. I have fixed it. I had to add the BindingFlags.Instance.