PDA

View Full Version : Does pascal have reflection?



slenkar
13-11-2010, 06:23 PM
I like to use reflection to make save game files with serialization. does this exist for pascal?

JSoftware
13-11-2010, 09:50 PM
In a way it does. You have RTTI for published properties of persistent objects(classes inheriting from TPersistent). Normal component streaming with the TStream makes serialization possible, but it's somewhat rigid. It'll do a lot, but not everything, and you have to be very careful about your design

So in conclusion; I wouldn't use it for anything complicated :)

slenkar
13-11-2010, 10:16 PM
thanks I found this http://wiki.freepascal.org/tiOPF
I may try to save objects in CSV format

If I had a Type called Car and some other types that extend it e.g. (ford ,honda,chevy)

could I use RTTI to get the names of the types(ford, honda etc.)
then create buttons with the names

then when the user clicks on a button use RTTI to create an instance of the selected car (ford, honda)?

Brainer
14-11-2010, 07:19 AM
If I had a Type called Car and some other types that extend it e.g. (ford ,honda,chevy)

could I use RTTI to get the names of the types(ford, honda etc.)

Yes. :)


type
Car = class(TObject)
end;

Honda = class(Car)
end;

var
HondaCar: Honda;
begin
HondaCar := Honda.Create();
try
ShowMessage(HondaCar.ClassName);
finally
FreeAndNil(HondaCar);
end;
end;

slenkar
14-11-2010, 05:45 PM
thanks, could i get a list of all the types that extend car?

I want to put a menu on the screen to let the player choose a car to drive

then when the player chooses a car, could i use the text (e.g. Ford, Honda) to create an instance of that type of car?

slenkar
24-11-2010, 04:33 AM
can I create an object with a text string of its name?

if not, could i have a little example program of turning an object into a stream and vice-versa?