System.Reflection如何访问类成员数据/信息? [英] How does System.Reflection works accessing a class members data /information?

查看:109
本文介绍了System.Reflection如何访问类成员数据/信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被告知不要使用反射。我真的想知道,是因为反射是昂贵的吗?如果没有什么是避免使用它的原因?



我目前和未来的项目可能还需要访问任何给定的类 - 成员信息。因为我有时需要列出字段&属性 - 值或声明名称。



所以我想知道的是:



反思如何工作?它如何获得信息? (简短说明)



为什么不推荐在应用程序中使用反射?如果您确实需要获取作为字段或属性值OR名称的必填信息,您可以使用System.Reflection 进行操作吗?



一些背景。



我目前项目中的用法sql server table-columns names或SQL - 表名。



我可以想到其他方法让它返回为 List< strings&



如果我真的知道为什么或如何不好是,使用反射



..我可以做出决定,如果我真的想避免它,我可能会找到一个替代方法(在这个特定的场景) p>

每当我需要(说)列表名称或者我可以做一次(访问数据)时,通过访问数据库(不可取),然后将其存储在文本文件或xml,如果我真的必须避免反射。



我知道一些更优雅的一个。这不是问题。 (再次这只是一个例子,因为可能有很多其他用例,你可能知道。)



更新



此问题已关闭,请帮助重新开启,并在下面投票重新开启



感谢。

解决方案

CLR程序集(可执行文件或动态链接库)通常包含关于其结构的元数据,这意味着关于类型,结构,方法,字段,其名称和一堆其他



反射是一个强大的工具,一些(通常是高级的)只能通过使用反射来实现。然而,它也引起了对安全性和封装的关注,因为你开始依赖于程序(或其他程序)的部分的实现,而你通常应该避免这些部分,只是信任这些部分给出的接口。另一个问题是性能,因为访问所有这些(最终文本)信息,与使用非反射方法(通常仍然使用偏移和大小信息)相反,程序减慢了。例如,您可以使用反射重新实现多态性,并绕过虚拟方法表。但后者比前者快许多倍。



使用反射,如果你必须,但不要使用它,如果你不必。它是那些非常强大的工具之一,人们建议不要使用它们,但如果你真的知道你在做什么,你可以使用它们。话虽如此,请记住,以错误的方式使用反射技术不仅会引起上述问题,而且往往会使您的代码难以维护。


I have been advised to refrain usage of Reflection. I really wanted to know, is it because Reflection is expensive? If not what is the reason to avoid using it?

My current and future projects might as well need to access any given class - members information. As I some times need to list fields & Properties - values or declaration name.

So What I would like to know, is:

How does Reflection work? How does it get to the information? (a short explanation will do)

And why it's not so recommended to use the reflection in application? If you do need to get required information being a field or properties values OR names, could you do it not by using System.Reflection?

Some background.

The usage in my current project(for example), is to list out a specific sql server table-columns names, or SQL - tables names .

I could think of other ways to have it returned as a List<strings>.

If I really knew why or how "bad" it is, to use reflection,

..I could then make a decision, if I really want to avoid it, as I might find an alternative approach (in this specific scenario).

Either by accessing database (not preferable) any time I need (say) list of tables names or I could do it once (access the data ), then store it in a text file or xml, if I really must avoid reflection.

I know of some even more elegant one, too. That's not the issue though. (again this is only an example as there could be many other use cases, you probably know that. )

update

this Question was closed please help reopen it , and vote 'reopen' below

thanks .

解决方案

A CLR assembly (executable or dynamic link library) typically contains metadata about its structure, which means information about the types, the structures, the methods, the fields, their names and a bunch of other information that, in "traditional" languages, were usually lost and replaced by offsets and size information only.

Reflection is a powerful tool and some (usually advanced) things can only be achieved using reflection. However, it also raises concerns about security and encapsulation, because you start relying on the implementations of the parts of your program (or other programs), while you should generally avoid those and just trust the interface those parts give. Another concern is performance, because to access all this kind of (ultimately textual) information, the program is slowed down, in contrast to using the non-reflective approaches (which usually still use offset and size information). For instance, you could re-implement polymorphism using reflection and bypass the virtual method table. But the latter is many times faster than the former.

Use reflection if you have to, but don't use it if you don't have to. It's one of those tools that are very powerful and people advise against using them, but you may use them if you really know what you're doing. That being said, please keep in mind that using reflection techniques in a wrong manner does not only raise the above issues, but tends to make your code significantly harder to maintain as well.

这篇关于System.Reflection如何访问类成员数据/信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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