What is reflection?

I am VERY new to ASP.NET. I come from the background of VB6 / ASP (classic) / SQL Server 2000. I read a lot about Visual Studio 2008 (installed it and scrolled it). I read about β€œthinking” and would like someone to explain, as much as possible, to the older technology developer that I wrote above, what Reflection is and why I will use it ... I'm having problems getting my head around that. Thanks!

+6
reflection metaprogramming
source share
5 answers

Reflection allows you to programmatically load an assembly, get a list of all types in an assembly, get a list of all the properties and methods in these types, etc.

As an example:

myobject.GetType().GetProperty("MyProperty").SetValue(myobject, "wicked!", null) 
+3
source share

Reflection is how you can explore the internal types of different types, usually without access (i.e. private, protected, etc.).

It is also used to dynamically load a DLL and access the types and methods defined in them without statically compiling them into your project.

In a nutshell: Reflection - your toolkit for peeping under the hood of a piece of code.

As for why you use it, it is usually used only in complex situations or in code analysis. Another common use is loading pre-compiled plugins into your project.

+8
source share

This allows internal objects to reflect the outside world (code that uses the specified objects).

Practical use in statically typed languages ​​such as C # (and Java) allows you to allow methods / members to be called at run time through a string (for example, the name of the method - you may not know the name of the method that you will use at compile time).

In the context of dynamic languages, I have not heard this term for so long (as a rule, you do not worry about it), and then, perhaps, iterate over the list of methods / members, etc.

+2
source share

Reflecting -.Net means manipulating or retrieving assembly, class, or method information at run time . For example, you can create a class at runtime, including its methods. As indicated by monoxide, reflection is used to dynamically load an assembly in the form of plug-ins or in predefined cases, it is used to create targeting for a .Net compiler .Net, such as IronPython.

Updated . You can refer to the metaprogramming section and related topics for more details.

+1
source share

When assembling any assembly in .NET (ASP.NET, Windows Forms, the command line, class library, etc.), the assembly also creates a series of "metadata definition tables" that store information about methods, fields, and types that correspond to types , fields and methods that you wrote in your code.

Classes in the .NET System.Reflection namespace allow you to list and interact with these tables, providing an β€œobject model” for querying and accessing elements in these tables.

One common use of Reflection is to provide extensibility (plugins) for your application. For example, Reflection allows you to dynamically load an assembly from a file path, request its types for a specific useful type (for example, an interface that your application can call), and then actually call a method on this external assembly.

Custom attributes also go hand in hand with reflection. For example, the NUnit unit testing framework allows you to specify a test class and test methods by adding the [Test] {TestFixture] attributes to your own code.

However, then the NUnit test runner must use Reflection to load your assembly, search for all instances of methods that have the test attribute, and then actually call your test.

This greatly simplifies it, however it gives you a good practical example of where Reflection is needed.

Reflection, of course, is powerful, but, nevertheless, it allows you to completely ignore the fundamental concept of access modifiers (encapsulation) in object-oriented programming.

For example, you can easily use it to get a list of private methods in a class and actually call them. For this reason, you need to think carefully about how and where you use it to avoid circumventing encapsulation and a very tight connection (of bad) code.

0
source share

All Articles