Delphi: RTTI and TObjectList<TObject>

≡放荡痞女 提交于 2019-12-05 16:36:49

Every generic instantiation is unique and has no relationship with other instantiations with respect to RTTI. Because Delphi can't instantiate generic types at runtime, there's no equivalent to e.g. .NET's GetGenericTypeDefinition. The best you can do is look at the shape of the type - e.g. does it implement GetEnumerator, Add etc.

This could also be flexible enough to handle general collection types, not just ones instantiated from TObjectList<T>. C# does something similar with its collection initializers - it looks for an Add method and inserts calls to it:

http://msdn.microsoft.com/en-us/library/bb384062.aspx

Delphi can instantiate generic types at runtime. The problem is, that generic types are generally not held in the runtime information. If you hold a global var of the specific type (TObjectList< TChildClass > in your case), instantiate (and free) it in initialization section the runtime information of that specific class wont be stripped by the linker and you will be able to instantiate it dynamically later. (You need to provide the full qualified name of the classes for context.findType() to work properly.) I spent some time figuring out how this can be done and I am definitely not happy with it, but since I haven't found another way I have to deal with it for now. If anybody knows a better way to do let me know.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!