|
|
Регистрация | << Правила форума >> | FAQ | Пользователи | Календарь | Поиск | Сообщения за сегодня | Все разделы прочитаны |
|
Опции темы | Поиск в этой теме | Опции просмотра |
#1
|
|||
|
|||
использование SetStrProp
Код:
uses TypInfo; type TMyType = class private FCaption: String; published property Caption: String read FCaption write FCaption; end; procedure TForm1.Button1Click(Sender: TObject); var X: TMyType; begin SetStrProp(Sender, 'Caption', 'Test'); ShowMessage('Wow'); X := TMyType.Create; try SetStrProp(X, 'Caption', 'Test'); // Ooops finally FreeAndNil(X); end; end; Почему я не могу использовать SetStrProp(X, 'Caption', 'Test'), и как это исправить? |
#2
|
|||
|
|||
вот так будет работать:
Код:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, TypInfo; type {$M+} TMyType = class private FCaption: String; published property Caption: String read FCaption write FCaption; end; {$M-} TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var X: TMyType; begin SetStrProp(Sender, 'Caption', 'Test'); ShowMessage('Wow'); X := TMyType.Create; try SetStrProp(X, 'Caption', 'Test'); // Ooops ShowMessage(X.Caption); finally FreeAndNil(X); end; end; end. The $M switch directive controls generation of runtime type information (RTTI). When a class is declared in the {$M+} state, or is derived from a class that was declared in the {$M+} state, the compiler generates runtime type information for fields, methods, and properties that are declared in a published section. If a class is declared in the {$M-} state, and is not derived from a class that was declared in the {$M+} state, published sections are not allowed in the class. Note that if a class is forward declared, the first declaration of the class must be declared with the $M switch. правильный вопрос содержит в себе 90% ответа |