Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/Compiler/Delphi/RTTI    [ Add a report in this area ]  
Report #:  85277   Status: Closed
Problem finding TRttiType for interfaces using TRttiContext.FindType
Project:  Delphi Build #:  14.0.3513.24210
Version:    15.0 Submitted By:   Joar Bølstad
Report Type:  Basic functionality failure Date Reported:  6/8/2010 11:26:12 PM
Severity:    Serious / Highly visible problem Last Updated: 3/20/2012 2:24:39 AM
Platform:    All platforms Internal Tracking #:   277267
Resolution: Fixed (Resolution Comments) Resolved in Build: : 15.0.3828.31970
Duplicate of:  None
Voting and Rating
Overall Rating: No Ratings Yet
0.00 out of 5
Total Votes: None
Description

RType := ctx.FindType('mynamespace.IMyPrettyLittleInterface');

in the main program assigns nil to RType, although the qualified name is known to be correct.

(See http://stackoverflow.com/questions/2997761/delphi-rtti-unable-to-find-interface for original SO post.)
Steps to Reproduce:
Consider the following program:

program rtti_sb_1;
{$APPTYPE CONSOLE}
uses
  SysUtils,
  Rtti,
  mynamespace in 'mynamespace.pas';
var
  ctx:      TRttiContext;
  RType:    TRttiType;
  MyClass:  TMyIntfClass;
begin
  ctx := TRttiContext.Create;
  MyClass := TMyIntfClass.Create;
  // This prints a list of all known types, including some interfaces.
  // Unfortunately, IMyPrettyLittleInterface doesn't seem to be one of them.
  for RType in ctx.GetTypes do
    WriteLn(RType.Name);
  // Finding the class implementing the interface is easy.
  RType := ctx.FindType('mynamespace.TMyIntfClass');
  // Finding the interface itself is not.
  RType := ctx.FindType('mynamespace.IMyPrettyLittleInterface');
  MyClass.Free;
  ReadLn;
end.

where mynamespace.pas contains

unit mynamespace;
interface
type
  IMyPrettyLittleInterface = interface

  ['{6F89487E-5BB7-42FC-A760-38DA2329E0C5}']

end;
  TMyIntfClass = class(TInterfacedObject, IMyPrettyLittleInterface)
  end;
implementation
end.

The line

RType := ctx.FindType('mynamespace.IMyPrettyLittleInterface');

in the main program assigns nil to RType, although the qualified name is known to be correct.

(See http://stackoverflow.com/questions/2997761/delphi-rtti-unable-to-find-interface for original SO post.)
Workarounds
Robert Love provided the following workaround:

Let the interface be "registered" with the TRttiContext through the following:

RType := ctx.GetType(TypeInfo(IMyPrettyLittleInterface));

If we then call

RType := ctx.FindType('mynamespace.IMyPrettyLittleInterface');

RType is assigned with the correct TRttiType.
Attachment
None
Comments

None

Server Response from: ETNACODE01