Delphi getting property value of a member from ClassType

Posted by Kayode Yusuf on Stack Overflow See other posts from Stack Overflow or by Kayode Yusuf
Published on 2010-04-19T15:04:47Z Indexed on 2010/04/19 15:13 UTC
Read the original article Hit count: 310

Filed under:
|
|

I am implementing a Boilerplate feature - allow users to Change descriptions of some components - like Tlabels - at run time. e.g.

TFooClass = Class ( TBaseClass)
 Label : Tlabel;
 ...
 End;

 Var FooClass : TFooClass;

...

At Design time, the value Label's caption property is say - 'First Name', when the application is run, there is a feature that allows the user to change the caption value to say 'Other Name'. Once this is changed, the caption for the label for the class instance of FooClass is updated immediately.

The problem now is if the user for whatever reason wants to revert back to the design time value of say 'First Name' , it seems impossible.

I can use the RTTIContext methods and all that but I at the end of the day, it seems to require the instance of the class for me to change the value and since this has already being changed - I seem to to have hit a brick wall getting around it.

My question is this - is there a way using the old RTTI methods or the new RTTIContext stuff to the property of a class' member without instantiating the class - i.e. getting the property from the ClassType definition.

This is code snippet of my attempt at doing that :

  c : TRttiContext;
   z : TRttiInstanceType;
   w : TRttiProperty;
 Aform : Tform;
  ....
 Begin
 .....

   Aform := Tform(FooClass);

   for vCount := 0 to AForm.ComponentCount-1 do begin
    vDummyComponent := AForm.Components[vCount];
    if IsPublishedProp(vDummyComponent,'Caption') then begin
      c := TRttiContext.Create;
       try
         z := (c.GetType(vDummyComponent.ClassInfo) as TRttiInstanceType);
         w := z.GetProperty('Caption');
          if w <> nil  then
             Values[vOffset, 1] := w.GetValue(vDummyComponent.ClassType).AsString
        .....
        .....

....
....

I am getting all sorts of errors and any help will be greatly appreciated.

© Stack Overflow or respective owner

Related posts about delphi

Related posts about rtti