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

  • SEARCH
  • Home
  • 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 6825123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:59:05+00:00 2026-05-26T21:59:05+00:00

First, a little explanation about my situation: I have a sample interface which is

  • 0

First, a little explanation about my situation:

I have a sample interface which is implemented by different classes, and these classes might not always have a shared ancestor:

  IMyInterface = interface
    ['{1BD8F7E3-2C8B-4138-841B-28686708DA4D}']
    procedure DoSomething;
  end;

  TMyImpl = class(TInterfacedPersistent, IMyInterface)
    procedure DoSomething;
  end;

  TMyImp2 = class(TInterfacedObject, IMyInterface)
    procedure DoSomething;
  end;

I also have a factory method which is supposed to create an instance of an object which implements my interface. My factory method receives the class name as its parameter:

  function GetImplementation(const AClassName: string): IMyInterface;

I tried two approaches to implement this factory method, the first one was using extended RTTI:

var
  ctx : TRttiContext;
  t : TRttiInstanceType;
begin
  t := ctx.FindType(AClassName).AsInstance;
  if Assigned(t) then
    Result := t.GetMethod('Create').Invoke(t.MetaclassType, []).AsInterface as IMyInterface;
end;

In this approach I am calling the default constructor which is fine in my scenario. The problem with this is, at runtime, I get an error telling me the object does not support IMyInterface. What’s more, the created object is not assigned to an interface variable; therefore, it will be leaked. I also tried returning the value using TValue.AsType method, but it gives me Access Violation:

function GetImplementation(const AClassName: string): IMyInterface;
var
  ctx : TRttiContext;
  rt : TRttiInstanceType;
  V : TValue;
begin
  rt := ctx.FindType(AClassName).AsInstance;
  if Assigned(rt) then
  begin
    V := rt.GetMethod('Create').Invoke(rt.MetaclassType, []);
    Result := V.AsType<IMyInterface>;
  end;
end;

.

The second approach I tried was using a generic dictionary to hold pairs of , and provide registration, unregistration methods:

  TRepository = class
  private
    FDictionary : TDictionary<string, TClass>;
  public
    constructor Create;
    destructor Destroy; override;
    function GetImplementation(const AClassName: string): IMyInterface;
    procedure RegisterClass(AClass: TClass);
    procedure UnregisterClass(AClass: TClass);
  end;

Here I implemented GetImplementation method as this:

function TRepository.GetImplementation(const AClassName: string): IMyInterface;
var
  Obj : TObject;
begin
  if FDictionary.ContainsKey(AClassName) then
  begin
    Obj := FDictionary[AClassName].Create;
    Obj.GetInterface(IMyInterface, Result);
  end;
end;

This works fine, and I can call DoSomething method using the returned value of GetImplementation, but it still has the memory-leak problem; Obj which is created here is not assigned to any interface variable; therefore, it is not reference-counted, and is leaked.

.

Now, my actual question:

So my question is, how can I safely create an instance of a class which implements my interface at runtime? I saw Delphi Spring Framework, and it provides such functionality in its Spring.Services unit, but it has its own reflection routines and lifetime management models. I am looking for a lightweight solution, not a whole 3rd-party framework to do this for me.

Regards

  • 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-26T21:59:06+00:00Added an answer on May 26, 2026 at 9:59 pm

    The first case using the RTTI give you a access violation because the TRttiContext.FindType(AClassName) cannot find the Rtti info for the classes which are not registered or used explicity in the app.

    So you can change your code to

    function GetImplementation(AClass: TClass): IMyInterface;
    var
      ctx : TRttiContext;
      t : TRttiInstanceType;
    begin
      t := ctx.GetType(AClass).AsInstance;
      if Assigned(t) then
        Result := t.GetMethod('Create').Invoke(t.MetaclassType, []).AsInterface As IMyInterface;
    end;
    

    and call in this way

    AClass:=GetImplementation(TMyImp2);
    

    Now if you want to use the Class name to invoke the class, using a list (like your TRepository class) to register the classes is a valid aproach. about the memory leak i’m pretty sure which is caused because the TMyImpl class is derived from the TInterfacedPersistent which not implement reference counting directly like the TInterfacedObject.

    This implementation of the the TRepository must works ok.

    constructor TRepository.Create;
    begin
      FDictionary:=TDictionary<string,TClass>.Create;
    end;
    
    destructor TRepository.Destroy;
    begin
      FDictionary.Free;
      inherited;
    end;
    
    function TRepository.GetImplementation(const AClassName: string): IMyInterface;
    var
      Obj : TObject;
    begin
      if FDictionary.ContainsKey(AClassName) then
      begin
        Obj := FDictionary[AClassName].Create;
        Obj.GetInterface(IMyInterface, Result);
      end;
    end;
    
    {
    or using the RTTI
    var
      ctx : TRttiContext;
      t : TRttiInstanceType;
    begin
      t := ctx.GetType(FDictionary[AClassName]).AsInstance;
      if Assigned(t) then
        Result := t.GetMethod('Create').Invoke(t.MetaclassType, []).AsInterface As IMyInterface;
    end;
    }
    
    procedure TRepository.RegisterClass(AClass: TClass);
    begin
      FDictionary.Add(AClass.ClassName,AClass);
    end;
    
    procedure TRepository.UnregisterClass(AClass: TClass);
    begin
      FDictionary.Remove(AClass.ClassName);
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First question on here so please be nice :) I know very little about
First, a little background. I have strings that resemble the following: ((Foo.Bar.StartsWith(A)) && (Foo.Bar.EndsWith(B)))
First, I have to admit I screwed up a little with CVS. I had
First of, I have read many explanations on SO and blogs about covariance and
I have some little test for Ruby On Rails 3.2.1 Here is explanation for
First a little background: I have an Event model that has various event_type s.
First, a little explanation of why I'm asking this question in the first place:
First a little intro: Last year i wrote this http://dragan.yourtree.org/code/canvas-3d-graph/ Now, i want to
First a little background, I'm using jdk 1.6. I've got a 2 column table
A little background first. I've been tasked with encrypting files with a Powershell script

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.