Reflection by Delphi for .NET
Reflection is
all about the ability to examine the capabilities of classes at runtime. Most of
the objects involved in reflection can be found in System.Reflection
Reflection
enables you to take an object and examine its methods, properties, constructors,
fields, events and other members.
Reflection is
centered around System.Type,
this object describes a type, and in most cases this type will represent a class
inside an assembly, represented by System.Reflection.Assembly.
System.Type
provides methods like GetMethods and GetProperties
to return member information. Looking deeper into the results (for example,
System.Reflection.MethodInfo) can tell you the
parameters and even let you call into the member through Invoke.
The following
image represents the ILDASM tool (Ships with the .NET Framework),
it mainly is a reflector of Assemblies. It allows you to load any valid .NET
Assembly and dive into its properties, methods and other members to discover its
characteristics and its run time type information.

Better
yet, if you download the Lutz Roeders .NET Reflector from
http://www.aisto.com/roeder/dotnet
You will
be able to do everything ILDASM does and much more. See the image below to
examine the capabilities of this reflector tool, where the system is hooked up
to the MSDN and a XML documentation viewer.

Finally,
some code to allow you to build your own Reflector in
Delphi
for .NET
This code
is simple enough to allow you to load a .NET Assembly, in this case we load
mscorlib.dll but you can change that to any .NET Assembly you wish to examine.
Concentrate
on the following tips:
- How to
access Environment Variables
`
Environment.GetEnvironmentVariable('systemroot');
- How to
load Assemblies `
Assembly.LoadFrom();
How to type cast arrays
`
System.Array();
How to dig into for loops to getTypes,
GetProperties, GetMethods,
etc (This will change once ForEach equivalent
makes it into the language for the .NET release of
Delphi.
The complete source code for this sample can be downloaded
from
http://codecentral.borland.com/codecentral/ccWeb.exe/listing?id=19426
procedure TReflectionForm.btnLoad_Click(Sender: TObject; E: EventArgs);
var
SysRoot: String;
Assm: Assembly;
T: array of System.Type;
i, iprop: integer;
PropertyCount, MethodCount, FieldCount, EventCount: integer;
TN, TNProp, TNMethod, TNEvent, TNField: TreeNode;
begin
SysRoot:= Environment.GetEnvironmentVariable('systemroot');
try
Assm:= Assembly.LoadFrom(SysRoot + 'Microsoft.NETFrameworkv1.1.4322mscorlib.dll');
SetLength(t, System.Array(Assm.GetTypes()).Length);
t:= Assm.GetTypes();
for i:= 0 to System.Array(Assm.GetTypes()).Length - 1 do
begin
TN:= TV.Nodes.Add(t[i].Name);
PropertyCount := PropertyCount + System.Array(t[i].GetProperties()).GetUpperBound(0) + 1;
TNProp:= TN.Nodes.Add('Properties');
for iprop:= 0 to System.Array(t[i].GetProperties()).Length - 1 do
TNProp.Nodes.Add(System.Array(t[i].GetProperties()).GetValue(iprop).tostring());
MethodCount:= MethodCount + System.Array(t[i].GetMethods()).GetUpperBound(0) + 1;
TNMethod:= TN.Nodes.Add('Methods');
for iprop:= 0 to System.Array(t[i].GetMethods()).Length - 1 do
TNMethod.Nodes.Add(System.Array(t[i].GetMethods()).GetValue(iprop).tostring());
FieldCount:= FieldCount + System.Array(t[i].GetFields()).GetUpperBound(0) + 1;
TNField:= TN.Nodes.Add('Fields');
for iprop:= 0 to System.Array(t[i].GetFields()).Length - 1 do
TNField.Nodes.Add(System.Array(t[i].GetFields()).GetValue(iprop).tostring());
EventCount:= EventCount + System.Array(t[i].GetEvents()).GetUpperBound(0) + 1;
TNEvent:= TN.Nodes.Add('Events');
for iprop:= 0 to System.Array(t[i].GetEvents()).Length - 1 do
TNEvent.Nodes.Add(System.Array(t[i].GetEvents()).GetValue(iprop).tostring());
end;
except
MessageBox.Show('Kaboom!! Problems loading the assembly'); //implement your own exception handling here.
end;
Self.Text:= 'Properties=' + IntToStr(PropertyCount) + ' Methods=' + IntToStr(MethodCount) + ' Fields='
+ IntToStr(FieldCount) + ' Events=' + IntToStr(EventCount);
stBar.Text :='Done';
end;
If you are not running the latest .NET Framework 1.1 Beta, please change the code to load from the "v1.0.3705" instead of "v1.1.4322"
Add some more to this code and play around with Reflection and see how easy it is to dig in and reflect information back from any .NET Assembly. In future, more advanced articles, I will show how to invoke methods in .NET Assemblies using Reflection, which is WAY COOL!
Happy New Year and write to you later J
About Falafel Software Inc:
Falafel Software is all about making the most of software development technology
in order to complete the project on time and on budget with best possible user
experience. Falafel Software offers a comprehensive suite of software development
solutions ranging from strategy to design to implementation that businesses
need in order to realize high returns on their investment.

Copyright ) 2003 Alain Tadros, Falafel
Software Inc.
ALL RIGHTS RESERVED. NO PART OF THIS DOCUMENT CAN BE COPIED IN ANY FORM WITHOUT
THE EXPRESS, WRITTEN CONSENT OF THE AUTHOR.