How to access RTTI in a class constructor?

Johan

This code is not allowed:

class constructor TOmniMultiPipelineStage.Create;
var
  RTTIType: TRttiType;
begin
  RTTIType:= TRttiContext.GetType(self);
end;

[dcc32 Error] OtlParallel.pas(5040): E2003 Undeclared identifier: 'self'

The variant is also not allowed:

class constructor TOmniMultiPipelineStage.Create;
var
  RTTIType: TRttiType;
begin
  //Not really what I want because I want the actual type of the class
  //Not a fixed ancestor type 
  RTTIType:= TRttiContext.GetType(TOmniMultiPipelineStage);
end;

[dcc32 Error] OtlParallel.pas(5039): E2076 This form of method call only allowed for class methods or constructor

How do I get RTTI info on a class in its class constructor?

Note to self: loop over all descendants of a class: Delphi: At runtime find classes that descend from a given base class?

David Heffernan

Use the ClassInfo class method of TObject:

class constructor TMyClass.ClassCreate;
var
  ctx: TRttiContext;
  typ: TRttiType;
begin
  typ := ctx.GetType(ClassInfo);
end;

Note that I also fixed the syntax of your call to GetType, which is an instance method, and so must be called on an instance of TRttiContext.

A bigger problem for you is that class constructors are not going to be of use to you. A class constructor is static. They execute once only, for the type that defines them. They do not execute in the context of derived classes, as you are clearly expecting.

And likewise for class vars, which you discuss in the comments. There is but a single instance of a class var. You are expecting and hoping that there will be new instances for each derived class.

So whilst ClassInfo answers the question you asked, it won't be of any real use to you.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Java static constructor access in child class

From Dev

How do I access a private constructor in a separate class?

From Dev

How do I access an array list inside of a class constructor in java?

From Dev

How can I access a private class' atributes from a function that the constructor is taking as parameter?

From Dev

How I can access a private static member variable via the class constructor?

From Dev

Unable to access RTTI for SmartMobileStudio

From Dev

How does RTTI work?

From Dev

Kotlin: How to access properties in constructor

From Dev

How to determine using Rtti, if a field from a class is a Record

From Dev

How to pass a comparator to the constructor of a class

From Dev

Call a protected method (constructor) via RTTI

From Dev

Protected access modifier use on Class's Constructor

From Dev

Access subclass' property from base class' constructor

From Dev

How to access base class constructor when using CRTP

From Dev

How to extend a class with a different constructor?

From Dev

Access constructor variables from another constructor in the same class

From Dev

Java - How to allow all methods of a class to access an array initialized in constructor?

From Dev

Unable to access RTTI for SmartMobileStudio

From Dev

How do I access an inner class constructor from a derived class?

From Dev

How to access class properties outside the constructor

From Dev

How to access dynamic array inside a class constructor?

From Dev

how to access parent class constructor into child class in codeigniter

From Dev

How to access a private constructor from controller to model class in mvc 4

From Dev

Python access derived class attributes in parent constructor

From Dev

How to access the attributes of an object instantiated via a constructor in an abstract class, from an overridden method in a concrete child class?

From Dev

How to access the constructor value outside the constructor

From Dev

How to access the property of the constructor?

From Dev

Kotlin Constructor from Java Super Class Constructor Access

From Dev

How to templating a class constructor

Related Related

  1. 1

    Java static constructor access in child class

  2. 2

    How do I access a private constructor in a separate class?

  3. 3

    How do I access an array list inside of a class constructor in java?

  4. 4

    How can I access a private class' atributes from a function that the constructor is taking as parameter?

  5. 5

    How I can access a private static member variable via the class constructor?

  6. 6

    Unable to access RTTI for SmartMobileStudio

  7. 7

    How does RTTI work?

  8. 8

    Kotlin: How to access properties in constructor

  9. 9

    How to determine using Rtti, if a field from a class is a Record

  10. 10

    How to pass a comparator to the constructor of a class

  11. 11

    Call a protected method (constructor) via RTTI

  12. 12

    Protected access modifier use on Class's Constructor

  13. 13

    Access subclass' property from base class' constructor

  14. 14

    How to access base class constructor when using CRTP

  15. 15

    How to extend a class with a different constructor?

  16. 16

    Access constructor variables from another constructor in the same class

  17. 17

    Java - How to allow all methods of a class to access an array initialized in constructor?

  18. 18

    Unable to access RTTI for SmartMobileStudio

  19. 19

    How do I access an inner class constructor from a derived class?

  20. 20

    How to access class properties outside the constructor

  21. 21

    How to access dynamic array inside a class constructor?

  22. 22

    how to access parent class constructor into child class in codeigniter

  23. 23

    How to access a private constructor from controller to model class in mvc 4

  24. 24

    Python access derived class attributes in parent constructor

  25. 25

    How to access the attributes of an object instantiated via a constructor in an abstract class, from an overridden method in a concrete child class?

  26. 26

    How to access the constructor value outside the constructor

  27. 27

    How to access the property of the constructor?

  28. 28

    Kotlin Constructor from Java Super Class Constructor Access

  29. 29

    How to templating a class constructor

HotTag

Archive