如何将类名作为字符串转换为类? [英] How to convert classname as string to a class?

查看:140
本文介绍了如何将类名作为字符串转换为类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在字符串列表中有类名。例如,可能是
'TPlanEvent','TParcel','TCountry'等。

I have classnames in a stringlist. For example it could be 'TPlanEvent', 'TParcel', 'TCountry' etc.

现在我想通过循环列表来查找大小。

Now I want to find out the sizes by looping the list.

它的工作原理是:

Size := TCountry.InstanceSize;

但是我想要这样:

for i := 0 to ClassList.Count - 1 do
  Size := StringToClass(ClassList[i]).InstanceSize;

很明显,我的问题是写什么而不是方法StringToClass来将字符串转换为类。

Obviously my question is what to write instead of method StringToClass to convert the string to a class.

推荐答案

由于使用的是字符串列表,因此您也可以在其中存储类:

Since you're using a stringlist you can store the classes there, too:

var
  C: TClass;

  StringList.AddObject(C.ClassName, TObject(C));

...

for I := 0 to StringList.Count - 1 do
  Size := TClass(StringList.Objects[I]).InstanceSize;

...

这篇关于如何将类名作为字符串转换为类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆