Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6001739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:52:40+00:00 2026-05-23T00:52:40+00:00

for a framework I wrote a wrapper which takes any object, interface or record

  • 0

for a framework I wrote a wrapper which takes any object, interface or record type to explore its properties or fields. The class declaration is as follows:

TWrapper<T> = class 
private
  FType : TRttiType;
  FInstance : Pointer;
  {...}
public
  constructor Create (var Data : T);
end;

In the constructor I try to get the type information for further processing steps.

constructor TWrapper<T>.Create (var Data : T);
begin
FType := RttiCtx.GetType (TypeInfo (T));
if FType.TypeKind = tkClass then
  FInstance := TObject (Data)
else if FType.TypeKind = tkRecord then
  FInstance := @Data
else if FType.TypeKind = tkInterface then
  begin
  FType := RttiCtx.GetType (TObject (Data).ClassInfo); //<---access violation
  FInstance := TObject (Data);
  end
else
  raise Exception.Create ('Unsupported type');
end;

I wonder if this access violation is a bug in delphi compiler (I’m using XE).
After further investigation I wrote a simple test function, which shows, that asking for the class name produces this exception as well:

procedure TestForm.FormShow (Sender : TObject);
var
  TestIntf : IInterface;
begin
TestIntf    := TInterfacedObject.Create;
OutputDebugString(PChar (TObject (TestIntf).ClassName)); //Output: TInterfacedObject
Test <IInterface> (TestIntf);
end;

procedure TestForm.Test <T> (var Data : T);
begin
OutputDebugString(PChar (TObject (Data).ClassName)); //access violation
end;

Can someone explain me, what is wrong? I also tried the procedure without a var parameter which did not work either. When using a non generic procedure everything works fine, but to simplify the use of the wrapper the generic solution would be nice, because it works for objects and records the same way.

Kind regards,

Christian

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:52:41+00:00Added an answer on May 23, 2026 at 12:52 am

    Your code contains two wrong assumptions:

    • That you can obtain meaningful RTTI from Interfaces. Oops, you can get RTTI from interface types.
    • That a Interface is always implemented by a Delphi object (hence your attempt to extract the RTTI from the backing Delphi object).

    Both assumptions are wrong. Interfaces are very simple VIRTUAL METHOD tables, very little magic to them. Since an interface is so narrowly defined, it can’t possibly have RTTI. Unless of course you implement your own variant of RTTI, and you shouldn’t. LE: The interface itself can’t carry type information the way an TObject does, but the TypeOf() operator can get TypeInfo if provided with a IInterface

    Your second assumption is also wrong, but less so. In the Delphi world most interfaces will be implemented by Delphi objects, unless of course you obtain the interface from a DLL written in an other programming language: Delphi’s interfaces are COM-compatible, so it’s implementations can be consumed from any other COM-compatible language and vice versa. But since we’re talking Delphi XE here, you can use this syntax to cast an interface to it’s implementing object in an intuitive and readable way:

    TObject := IInterface as TObject;
    

    that is, use the as operator. Delphi XE will at times automagically convert a hard cast of this type:

    TObject := TObject(IInterface);
    

    to the mentioned "as" syntax, but I don’t like this magic because it looks very counter-intuitive and behaves differently in older versions of Delphi.

    Casting the Interface back to it’s implementing object is also wrong from an other perspective: It would show all the properties of the implementing object, not only those related to the interface, and that’s very wrong, because you’re using Interfaces to hide those implementation details in the first place!

    Example: Interface implementation not backed by Delphi object

    Just for fun, here’s a quick demo of an interface that’s not backed by an Delphi object. Since an Interface is nothing but an pointer to a virtual method table, I’ll construct the virtual method table, create a pointer to it and cast the the pointer to the desired Interface type. All method pointers in my fake Virtual Method table are implemented using global functions and procedures. Just imagine trying to extract RTTI from my i2 interface!

    program Project26;
    
    {$APPTYPE CONSOLE}
    
    uses
      SysUtils;
    
    type
    
      // This is the interface I will implement without using TObject
      ITestInterface = interface
      ['{CFC4942D-D8A3-4C81-BB5C-6127B569433A}']
        procedure WriteYourName;
      end;
    
      // This is a sample, sane implementation of the interface using an
      // TInterfacedObject method
      TSaneImplementation = class(TInterfacedObject, ITestInterface)
      public
        procedure WriteYourName;
      end;
    
      // I'll use this record to construct the Virtual Method Table. I could use a simple
      // array, but selected to use the record to make it easier to see. In other words,
      // the record is only used for grouping.
      TAbnormalImplementation_VMT = record
        QueryInterface: Pointer;
        AddRef: Pointer;
        ReleaseRef: Pointer;
        WriteYourName: Pointer;
      end;
    
    // This is the object-based implementation of WriteYourName
    procedure TSaneImplementation.WriteYourName;
    begin
      Writeln('I am the sane interface implementation');
    end;
    
    // This will implement QueryInterfce for my fake IInterface implementation. All the code does
    // is say the requested interface is not supported!
    function FakeQueryInterface(const Self:Pointer; const IID: TGUID; out Obj): HResult; stdcall;
    begin
      Result := S_FALSE;      
    end;
    
    // This will handle reference counting for my interface. I am not using true reference counting
    // since there is no memory to be freed, si I am simply returning -1
    function DummyRefCounting(const Self:Pointer): Integer; stdcall;
    begin
      Result := -1;
    end;
    
    // This is the implementation of WriteYourName for my fake interface.
    procedure FakeWriteYourName(const Self:Pointer);
    begin
      WriteLn('I am the very FAKE interface implementation');
    end;
    
    var i1, i2: ITestInterface;
        R: TAbnormalImplementation_VMT;
        PR: Pointer;
    
    begin
      // Instantiate the sane implementation
      i1 := TSaneImplementation.Create;
    
      // Instantiate the very wrong implementation
      R.QueryInterface := @FakeQueryInterface;
      R.AddRef := @DummyRefCounting;
      R.ReleaseRef := @DummyRefCounting;
      R.WriteYourName := @FakeWriteYourName;
      PR := @R;
      i2 := ITestInterface(@PR);
    
      // As far as all the code using ITestInterface is concerned, there is no difference
      // between "i1" and "i2": they are just two interface implementations.
      i1.WriteYourName; // Calls the sane implementation
      i2.WriteYourName; // Calls my special implementation of the interface
    
      WriteLn('Press ENTER to EXIT');
      ReadLn;
    end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any class in the .NET framework that can read/write standard .ini files:
I'm revisiting my class tracking (dirty logic), which I wrote last year. Currently I
I have a REST web service class which i call HttpRequest using curl.I wrote
I try to write a XML<>Object Mapper for my Entity Framework Code First class.
We use an enterprise framework that we wrote to facilitate all sorts of company
Current situation: I have the current version of my MVC Framework which uses classes
How to check the .net framework version on start of WinForms application that wrote
I have an Application class. Entity framework created a navigation property called Assistants .
Is there an ACID framework for bulk data persistance, which would also allow some
I have a custom UI control which has a JavaScript class written around the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.