C#:有人可以解释反思的实用性吗? [英] C#: Can someone explain the practicalities of reflection?

查看:86
本文介绍了C#:有人可以解释反思的实用性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我尝试搜索,希望有人对此有一个很好的解释,没有运气。



我问了我的另一个朋友一个不同的问题(我问过



我是C#领域的新手,曾经是一名VB.net业余程序员(以及JavaScript,ActionScript和C),并且正在尽我最大的努力去掌握这些高级概念。



有很多哲学上的答案-应用程序自视-但是他们没有提供任何实际提示,说明实际发生的情况或在该情况下如何使用它。



那么,什么是反思,为什么呢?重要,为什么/如何使用?

解决方案

反射功能可以确定事物并在运行时执行代码。



如果您不想使用它,则不必使用,但是它对于动态行为非常方便。



例如:



a)您可以使用反射通过加载外部配置文件并基于该文件启动服务来配置应用程序。您的应用程序不必事先了解实现这些服务的类,只要它们符合特定的接口或API。



b)使用反射,您可以即时生成类和代码,因为程序员不必显式创建所有所需的代码,因此可以简化某些编程任务。 p)

c)对于通过检查代码工作的程序,反射也是非常宝贵的。一个示例就是IDE或UI设计器。



d)Reflection帮助您减少样板代码。



e)反射很容易在代码中定义小型领域特定语言(DSL)。


So I tried searching SO hoping someone had a good explanation of this, with no luck.

I asked another friend of mine a different question (which I've now forgotten) and his answer was simply "reflection" before he signed off.

I am still very new to the C# world, having been an amateur VB.net programmer (also JavaScript, ActionScript, and C), and am trying as hard as I can to grasp these advanced concepts.

There's lots of philosophical answers -- "application looking at itself" -- but they don't provide any practical hints on what is actually going on or how it is used within that context.

So, what is reflection, why is it important, and why/how do I use it?

解决方案

Reflection provides the ability to determine things and execute code at runtime.

You don't have to use it if you don't want to, but it is extremely handy for dynamic behavior.

For example:

a) You can use reflection to configure your application by loading an external configuration file and starting services based on it. Your application wont have to know in advance about the classes that implement those services, as long as they conform to a specific interface or API.

b) Using reflection you can generate classes and code on the fly, which simplifies certain programming tasks since the programmer does not have to explicitly create all the needed code.

c) Reflection is also invaluable for programs that work by examining code. An example of that would be an IDE or a UI designer.

d) Reflection helps you reduce boilerplate code.

e) Reflection is handy for defining mini Domain Specific Languages (DSL) in your code.

这篇关于C#:有人可以解释反思的实用性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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