Reflection....

U

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.
 
R

Richard Grimes

UJ said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top