views:

690

answers:

5

I am VERY new to ASP.NET. I come from a VB6 / ASP (classic) / SQL Server 2000 background. I am reading a lot about Visual Studio 2008 (have installed it and am poking around). I have read about "reflection" and would like someone to explain, as best as you can to an older developer of the technologies I've written above, what exactly Reflection is and why I would use it... I am having trouble getting my head around that. Thanks!

+7  A: 

Reflection is how you can explore the internals of different Types, without normally having access (ie. private, protected, etc members).

It's also used to dynamically load DLL's and get access to types and methods defined in them without statically compiling them into your project.

In a nutshell: Reflection is your toolkit for peeking under the hood of a piece of code.

As to why you would use it, it's generally only used in complex situations, or code analysis. The other common use is for loading precompiled plugins into your project.

Matthew Scharley
Thank you. This clears it up... a lot. Still a bit confused. But I am much more clear on it now. Something like "metacode"? (I made that word up but in the generic sense of "meta..."?
By the normal definition of "meta...", you would mean code that describes code. That's what the Reflection namespace exposes, that description. Code that uses Reflection uses that information in some way, whether it's info about it's own assembly,
Matthew Scharley
or some other assembly you have loaded, dynamically or not.
Matthew Scharley
it seems VERY cool and I might have some ideas already.
+3  A: 

It allows the internals of an object to be reflected to the outside world (code that is using said objects).

A practical use in statically typed languages like C# (and Java) is to allow invocation of methods/members at runtime via a string (eg the name of the method - perhaps you don't know the name of the method you will use at compile time).

In the context of dynamic languages I haven't heard the term as much (as generally you don't worry about the above), other then perhaps to iterate through a list of methods/members etc...

Michael Neale
So it is not just ASP.NET..... Where else is this supported AND do I need to worry about this as I just start off with "dot net" ?
I don't think you need to worry about it if you are just starting out. It is part of the .NET framework though so is relevant to all .NET development (not just ASP.NET)
spoon16
+2  A: 

Reflection is .Net's means to manipulate or extract information of an assembly, class or method at run time. For example, you can create a class at runtime, including it's methods. As stated by monoxide, reflection is used to dynamically load assembly as plugins, or in advance cases, it is used to create .Net compiler targeting .Net, like IronPython.

Updated: You may refer to the topic on metaprogramming and its related topics for more details.

OnesimusUnbound
+3  A: 

Reflection lets you programmatically load an assembly, get a list of all the 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)
Justice
OK. That is clear.
Very likely that you do not need to perform this type of operation if you are "just starting out with asp.net" :)
spoon16
You probably don't need to use it immediately, yeah. But it's sometimes helpful to know what other people are talking about!
Justice
+1  A: 

When you build any assembly in .NET (ASP.NET, Windows Forms, Command line, class library etc), a number of meta-data "definition tables" are also created within the assembly storing information about methods, fields and types corresponding to the types, fields and methods you wrote in your code.

The classes in System.Reflection namespace in .NET allow you to enumerate and interate over these tables, providing an "object model" for you to query and access items in these tables.

One common use of Reflection is providing extensibility (plug-ins) to your application. For example, Reflection allows you to load an assembly dynamically from a file path, query its types for a specific useful type (such as an Interface your application can call) and then actually invoke 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 indicate a testing class and test methods by adding [Test] {TestFixture] attributes to your own code.

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

This is simplifying it a lot, however it gives you a good practical example of where Reflection is essential.

Reflection certainly is powerful, however be ware that it allows you to completely disregard the fundamental concept of access modifiers (encapsulation) in object oriented programming.

For example you can easily use it to retrieve 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 bypassing encapsulation and very tightly coupling (bad) code.

Ash