Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Reflection

Namespace Nexamas.UI.PropertyGrid

    ''' <summary>
    ''' Reflection boundary for MASPropertyGrid object inspection. It reads public instance properties only.
    ''' </summary>
    Friend NotInheritable Class MASPropertyGridObjectInspector
        Private Sub New()
        End Sub

        Friend Shared Function Inspect(source As Object) As IReadOnlyList(Of MASPropertyGridPropertyState)
            Dim result As New List(Of MASPropertyGridPropertyState)()
            If source Is Nothing Then Return result

            Dim sourceType As Type = source.GetType()
            Dim fallbackCategory As String = MASPropertyGridText.Normalize(sourceType.Name, "Object")
            Dim properties As PropertyInfo() = sourceType.GetProperties(BindingFlags.Instance Or BindingFlags.Public)

            For Each propertyInfo As PropertyInfo In properties.OrderBy(Function(item) item.Name, StringComparer.OrdinalIgnoreCase)
                If propertyInfo Is Nothing Then Continue For
                If Not propertyInfo.CanRead Then Continue For
                If propertyInfo.GetIndexParameters().Length > 0 Then Continue For
                If propertyInfo.GetGetMethod(False) Is Nothing Then Continue For

                result.Add(MASPropertyGridPropertyState.CreateBound(source, propertyInfo, fallbackCategory))
            Next

            Return result
        End Function

    End Class

End Namespace
