David O'Brien asked us:

> I'm trying to get a list of properties and methods for any given object
> class, and am obviously looking in exactly the wrong places...

Unfortunately this are is a little undocumented, but what you need to use is
the Delphi RTTI (Run Time Type Information) API's that are exposed in the
TypInfo unit. The best documentation in this are is available in Ray
Lischner's "Secrets of Delphi 2" Chapter 7.

[Do other people on the group think I'm a broken record for recommending
this book so often? 8-)]

But to help you along here's some example code from that copies all the
properties from one object to another object where they have the same name,
have the same type and both have appropioate access methods defined.
(Apologies for any formatting mess made by my mailer).

procedure CopyProperties(Source, Dest: TObject);
var
  TypeData: PTypeData;
  PropList: PPropList;
  DestProp: PPropInfo;
  I: Integer;
begin
  TypeData := GetTypeData(Source.ClassInfo);
  GetMem(PropList, TypeData^.PropCount * SizeOf(PPropInfo));
  try
    GetPropInfos(Source.ClassInfo, PropList);
    for I := 0 to TypeData^.PropCount - 1 do
      begin
        DestProp := GetPropInfo(Dest.ClassInfo, PropList[I].Name);
        if (DestProp <> nil) and
           (DestProp.PropType^.Kind = PropList[I].PropType^.Kind) and
            (PropList[I].GetProc <> nil) and (DestProp.SetProc <> nil) then
          case DestProp.PropType^.Kind of
            tkInteger,
            tkChar,
            tkEnumeration,
            tkWChar,
            tkSet,
            tkClass:
              SetOrdProp(Dest, DestProp, GetOrdProp(Source,
                PropList[I]));
            tkFloat:
              SetFloatProp(Dest, DestProp, GetFloatProp(Source,
                PropList[I]));
            tkString,
            tkLString,
            tkWString:
              SetStrProp(Dest, DestProp, GetStrProp(Source,
                PropList[I]));
            tkMethod:
              SetMethodProp(Dest, DestProp, GetMethodProp(Source,
                PropList[I]));
            tkVariant:
              SetVariantProp(Dest, DestProp, GetVariantProp(Source,
                PropList[I]));
            tkInt64:
              SetInt64Prop(Dest, DestProp, GetInt64Prop(Source,
                PropList[I]));
          end
    end
  finally
    FreeMem(PropList)
  end
end;

Have a look at the interface of TypInfo, concider the code an dI think that
you should get the idea of how you access all of the published properties.
If you are going to play the game more than this you really need a book
(like Ray's) that will document all of the interesting bits of the RTTI
mechanism for you and save all the mucking about trying to figure it out for
yourself.

Cheers, Max.

========================================================================
Max Nilson                                         Phone: +64-9-278 4931
Profax International Limited    "Most people would rather die than think
use Std_Disclaimer;              - in fact, they do so" Bertrand Russell
========================================================================

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to