Reflection. What can we achieve using it?

2020-02-03 06:47发布

I'm reading and learning about reflection in C#. It would be fine to know how can it help me in my daily work, so I want people with more experience than me tell me samples or ideas about what kinds of things can we achieve using it, or how can we reduce de amount of code that we write.

Thanks.

14条回答
做自己的国王
2楼-- · 2020-02-03 07:41

I used reflection to facilitate the translation of controls like labels and buttons on our forms. Using reflection I would go through all the controls on my form and write the controls name,text and title to a XML file. After the controls title and text are translated in the XML, the file is read back in setting every control's title and text found in the XML to it's translated values.
Our forms need to be translated to several different languages and using reflection helped us to save a lot of time.

查看更多
女痞
3楼-- · 2020-02-03 07:42

A classic use I have for it in using Red Gate .Net Reflector to better understand the mechanics of the .Net framework itself. Ever wondered how or why a particularly framework object works the way it does or have you ever been a bit stumped as too why somethng doesn't work in the manner you thought it would?

Sometimes the documentation can be a little shabby to say the least but by using reflection and the tool by Redgate you can sniff about in the frameworks code to better understand the how/why/what of it all.

More than a few bugs have been found in the CLRs code via "MS outsiders" using reflection.

查看更多
登录 后发表回答