PrivateObject 而不是生产代码中的反射 [英] PrivateObject instead of Reflection in a production code

查看:47
本文介绍了PrivateObject 而不是生产代码中的反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

反射"是指使用 System.Reflection 命名空间.

By the "Reflection" I mean using the System.Reflection namespace.

MSDN 关于 PrivateObject 类的说明:允许测试代码...".与 System.Reflection 相比,我更喜欢 PrivateObject 语法,所以我想知道是否有真正的理由不在生产代码中使用它,而仅将其保留用于单元测试?

MSDN says about the PrivateObject class: "Allows test code...". I like a PrivateObject syntax more than System.Reflection one so I wonder is there a real reason not to use it in a production code, and keep it for unittests only?

推荐答案

你不能使用 PrivateObject 来实际反射一个类型.换句话说:您不能使用它来获取类型的成员.
只有事先知道成员姓名才能使用.

You can't use PrivateObject to actually reflect on a type. In other words: You can't use it to get the members of a type.
You can only use it if you know the names of the members beforehand.

也就是说,如果这些限制在您的场景中无关紧要,我看不出有什么理由不允许您使用它.
该类是public,文档齐全且未弃用.

That said, I don't see a reason why you shouldn't be allowed to use it, if those constraints don't matter in your scenario.
The class is public, well documented and not deprecated.

这篇关于PrivateObject 而不是生产代码中的反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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