反射-在我自己的程序集中递归地迭代对象的属性(Vb.Net/3.5) [英] Reflection - Iterate object's properties recursively within my own assemblies (Vb.Net/3.5)

查看:78
本文介绍了反射-在我自己的程序集中递归地迭代对象的属性(Vb.Net/3.5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以帮助我-我在思考方面做得并不多,但了解基本原理.

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

我要做什么:

我正在开发一个类,该类收集有关本地系统,网络等的大量信息,以用于自动错误报告.不必每次添加新属性都需要更改测试工具,而是(理想情况下)希望能够将批次序列化为XML字符串,然后仅在文本框中显示它.

I'm in the process of developing a class that gathers a lot of information about the local system, network, etc... to be used for automated bug reporting. Instead of having to change my test harness every time I add a new property, I'd (ideally) like to be able to serialise the lot as an XML string and just display that in a textbox.

不幸的是,框架不会在只读属性(几乎是我的所有属性)上使用默认的XML序列化程序,因为它们无法正确地反序列化

Unfortunately, the Framework won't use the default XML serializer on read-only properties (which almost all of mine are) as they wouldn't deserialize properly

[我不确定我是否同意序列化的任何东西都必须可以反序列化的假设-MS说这是我设计的一种通过设计"的功能,我想我可以理解-也许有一个标签指示应该序列化还是有好处的吗?]

最初的方法是使属性可获取和可设置(在setter上有抛出异常),但是此后整理起来的工作量似乎有点多余,我希望属性在最终版本中为只读

The initial approach was to make properties gettable and settable (with a throw exception on the setter) but the amount of work tidying this up afterwards seems a little excessive and I would want the properties to be read-only in the final version.

我需要什么帮助:

我当前的计划是使用反射来递归地遍历我最顶层的收集类的每个(公共)属性.问题是,我见过的样本无法递归处理.此外,如果对象在我的程序集中之一中,我只想检查它的属性-否则,只需在其上调用.ToString.

My current plan is to use reflection to recursively iterate through each (public) property of my topmost gathering class. The problem is, the samples I've seen don't handle things recursively. Additionally, I only want to inspect an object's properties if it's in one of my assemblies - Otherwise just call .ToString on it.

如果我的检查不限于我的组件,我假设我将得到(说)一个字符串,然后该字符串包含一个Length,而Length则将具有.Tostring方法...

If I don't have the inspection limited to my assembly, I assume I'll get (say) a string which then contains a Length which in turn will have .Tostring method...

出于本项目的目的,我可以几乎保证我的代码中没有循环引用,因为这将仅用作开发工具,所以我也不会 >担心它会不时地运行.

For the purposes of this project, I can almost guarantee no circular references within my code and as this will only be used as a development tool so I'm not too concerned about it running amok now and then.

我会喜欢一些示例/建议.

I'd appreciate some examples/advice.

非常感谢.

推荐答案

这有望使您入门.它直接将树打印到控制台,因此您需要进行调整以输出XML.然后更改IsMyOwnType方法以过滤出您感兴趣的程序集,现在它只关心与自身相同的程序集中的类型.

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

这篇关于反射-在我自己的程序集中递归地迭代对象的属性(Vb.Net/3.5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆