Reflection - Iterate object's properties recursively within my own assemblies (Vb.Net/3.5)

后端 未结 2 535
北荒
北荒 2021-01-28 01:27

I wonder if anyone can help me - I\'ve not done much with reflection but understand the basic principles.

What I\'m trying to do:

I\'m in the pr

相关标签:
2条回答
  • 2021-01-28 02:06

    you extension version on c# to use on any object

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    
    namespace Extensions
    {
        public static class ObjectExtension
        {
            public static string ToStringProperties(this object o)
            {
                return o.ToStringProperties(0);
            }
        public static string ToStringProperties(this object o, int level)
        {
            StringBuilder sb = new StringBuilder();
            string spacer = new String(' ', 2 * level);
            if (level == 0) sb.Append(o.ToString());
            sb.Append(spacer);
            sb.Append("{\r\n");
            foreach (PropertyInfo pi in o.GetType().GetProperties())
            {
    
                if (pi.GetIndexParameters().Length == 0)
                {
                    sb.Append(spacer);
                    sb.Append("  ");
                    sb.Append(pi.Name);
                    sb.Append(" = ");
    
                    object propValue = pi.GetValue(o, null);
                    if (propValue == null)
                    {
                        sb.Append(" <null>");
                    } else {
                        if (IsMyOwnType(pi.PropertyType))
                        {
                            sb.Append("\r\n");
                            sb.Append(((object)propValue).ToStringProperties(level + 1));
                        } else{
                            sb.Append(propValue.ToString());
                        }
                    }
                    sb.Append("\r\n");
                }
            }
            sb.Append(spacer);
            sb.Append("}\r\n");
            return sb.ToString();
        }
        private static bool IsMyOwnType(Type t) 
    {
        return (t.Assembly == Assembly.GetExecutingAssembly());
    }
    }
    }
    
    0 讨论(0)
  • 2021-01-28 02:18

    This will hopefully get you started. It prints a tree directly to the console so you'll need to adjust to output XML. Then change the IsMyOwnType method to filter out the assemblies you're interested in, right now it only cares about types in the same assembly as itself.

    Shared Sub RecurseProperties(o As Object, level As Integer)
        For Each pi As PropertyInfo In o.GetType().GetProperties()
            If pi.GetIndexParameters().Length > 0 Then Continue For
    
            Console.Write(New String(" "c, 2 * level))
            Console.Write(pi.Name)
            Console.Write(" = ")
    
            Dim propValue As Object = pi.GetValue(o, Nothing)
            If propValue Is Nothing Then
                Console.WriteLine("<null>")
            Else
                If IsMyOwnType(pi.PropertyType) Then
                    Console.WriteLine("<object>")
                    RecurseProperties(propValue, level+1)
                Else
                    Console.WriteLine(propValue.ToString())
                End If
            End If
    
        Next
    End Sub
    
    Shared Function IsMyOwnType(t As Type) As Boolean
        Return t.Assembly Is Assembly.GetExecutingAssembly()
    End Function
    
    0 讨论(0)
提交回复
热议问题