By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,200 Members | 1,755 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,200 IT Pros & Developers. It's quick & easy.

Reflection....

P: n/a
UJ
Can somebody point me to an explanation of what Reflection is? I've tried
looking in the help and it's not very helpful.

TIA - Jeff.
Nov 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
You can start from
http://msdn.microsoft.com/msdnmag/is...s/default.aspx

"UJ" <fr**@nowhere.com> wrote in message
news:OH*************@TK2MSFTNGP10.phx.gbl...
Can somebody point me to an explanation of what Reflection is? I've tried
looking in the help and it's not very helpful.

TIA - Jeff.

Nov 22 '05 #2

P: n/a
UJ wrote:
Can somebody point me to an explanation of what Reflection is? I've
tried looking in the help and it's not very helpful.


..NET code is self describing, each class has metadata that describes the
members of the class, their types and parameters. This metadata is
stored in the assembly where the class is defined, so it is always
available. Metadata is a bit like a type library for a COM class, or a
header file for a C++ class.

The metadata format is actually a relational database: there are many
tables which refer to each other. To get access to this data you can use
the unmanaged COM object, so for example to get the types in an assembly
I ask for table 0x02 (TypeDef) and for each type there is a row that
lists the name, namespace, the start index in the MethodDef table (0x06)
for the methods this type implements and the index in the Field table
(0x04). To get the list of methods you use the index and read each row
in the MethodDef table until you get to the methods of the next type
(hence you need to read the method index of the next type before you do
this). The same can be said for fields. The metadata COM object just
reads the metadata tables directly from the assmbly file, and you can do
this yourseld, if you know the position of the tables in the file and
the metadata schema (the .NET spec define both).

Does this sound complicated? Well it isn't, but it is tedious.
Especially since the method definition table does not give an indication
of the type that implements the method, so to do this you have to
iterate through all types, get the range of methods that type implements
and see if your method is in that range.

So that is the physical representation of the metadata. Reflection is a
*logical* representation, and the framework has gone through all the
effort of reading the appropriate metadata tables. So if you have a
MethodInfo for a method you simply have to access the DeclaringType to
get the type that contains the method.

What's the use of reflection?

Well metadata is vitally important in .NET security because metadata
about the *called* type is added to the assembly of the calling type
(MemberRef, TypeRef tables) and when .NET calls the method it compares
the metadata in the calling assembly with the metadata of the type in
the assembly that is being called. These have to match *exactly* for the
method to be called. This prevents the problems that Win32 has with
GetProcAddress.

The main use that you will find for reflection is to have scripting-like
access. The Reflection API gives you information about a type *and* it
allows you to execute instances of that type.

Richard
--
http://www.grimes.demon.co.uk/workshops/fusionWS.htm
http://www.grimes.demon.co.uk/workshops/securityWS.htm
Nov 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.