如何将一个字符串实例化成一个类(50分)

  • 主题发起人 主题发起人 Michael deng
  • 开始时间 开始时间
M

Michael deng

Unregistered / Unconfirmed
GUEST, unregistred user!
如题:我想将某一字符串,如:'Tjob' 将其实例化为一个Tjob类(假设系统中已存在Tjob类,我想在XML配置文件中写入该类名:Tjob,读配置时,将其取出的'Tjob'变成类)
 
这你都想得到,有点意思
 
function FindClass(const ClassName: string): TPersistentClass;
 
原来回答过的问题:
如果不明白下面的代码,可以仔细看这个帖子:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3260876

根据字符动态创建form并在listview里显示一个节点,然后双击该节点显示该form

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, ShockwaveFlashObjects_TLB, AppEvnts, StdCtrls,
ComCtrls, ExtCtrls;

type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
ListView1: TListView;
procedure ListView1DblClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

TClassArray = array [1..5] of TPersistentClass;

var
Form1: TForm1;
implementation

uses Unit2,unit3,unit4,unit5,unit6;

{$R *.dfm}
const
formarray: TClassArray = (tform2,tform3,TForm4,tform5,tform6);


procedure TForm1.ListView1DblClick(Sender: TObject);
begin
if ListView1.Selected<>nil then
if assigned(ListView1.Selected.Data) then
Tform(ListView1.Selected.Data).Show;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
item:Tlistitem;
frm:Tform;
i:integer;
k:boolean;
pc:TPersistentClass;
begin
try
pc:=findclass(edit1.Text);
except
showmessage('未找到类型'+edit1.Text);
exit;
end;
k:=false;
for i:=0 to application.ComponentCount-1 do
if uppercase(application.Components.ClassName)=uppercase(edit1.Text) then
begin
frm:=Tform(application.Components);
k:=true;
break;
end;
if not k then
application.CreateForm(TComponentclass(pc),frm);
showmessage(frm.ClassName);
item:=ListView1.Items.Add;
item.Caption:=edit1.Text;
item.Data:=frm;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
RegisterClasses(formarray);
end;

end.
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1224849
请问怎样把字符串转化为一个类?

http://www.delphibbs.com/delphibbs/dispq.asp?lid=2420610
Delphi 的RTTI机制浅探

http://www.delphibbs.com/delphibbs/dispq.asp?lid=2421470
Delphi 的RTTI机制浅探(续)
 
后退
顶部