2

I want to create classes from the name of a type previously saved in database (QualifiedName). but when I called the function TRttiContext.FindType ('QualifiedName'), I found only a few classes

Note: All classes are public and all classes are in the same unit and AllClasses Inherits from TMyClassParent

Procedure TMyObjects.Load;
var s, typeName : string;            
ctx : TRttiContext;
  t: TRttiInstanceType;
  tp: TRttiType;
  o: TMyClassParent;
begin
 ctx := TRttiContext.Create;
 try
    While not Table.Eof do begin
      typeName := format('%s.%s',[FieldByName('UnitName').AsString,FieldByName('TypeName').AsString]);
     if trim(typeName) <> '' then begin
         tp := ctx.FindType(typeName);
        ///..here when I debug some Types exists ( tp <> nil ) and for others tp = nil...
        t := tp as TRttiInstanceType;
        if Assigned(t) then  begin 
            o :=  t.MetaClassType.Create;
         //...
         ///...
        end;
       ///....
   end;
   //....   
finally
   ctx.free;
end;

Is there anyOne who can Help me please, I am using Delphi XE5 and Sorry for my English..

relghers
  • 49
  • 3
  • OT: there's a built-in mechanism in RTL for this. You can register class by the [`RegisterClass`](http://docwiki.embarcadero.com/Libraries/XE5/en/System.Classes.RegisterClass) procedure and get it by the [`GetClass`](http://docwiki.embarcadero.com/Libraries/XE5/en/System.Classes.GetClass), or [`FindClass`](http://docwiki.embarcadero.com/Libraries/XE5/en/System.Classes.FindClass) functions. – TLama Oct 02 '14 at 21:00
  • 2
    Can the reason be found here: [Delphi 2010 RTTI - RttiContext.FindType](http://stackoverflow.com/q/3460382/576719). The classes may be excluded by the linker. – LU RD Oct 02 '14 at 21:04
  • Thanks I'am going to try this.. – relghers Oct 02 '14 at 21:04
  • 3
    And see @DavidHeffernan's solution [How can I make sure RTTI is available for a class without instantiating it?](http://stackoverflow.com/a/10613212/576719). – LU RD Oct 02 '14 at 21:38

1 Answers1

1

I solved the problem, in fact it was enough just to use at least once the class or make a reference to the class anywhere in the program to make sure that the compiler/linker cannot strip it from the executable The Solution Thanks to All

Community
  • 1
  • 1
relghers
  • 49
  • 3