Delphi Spring general framecode register

Using the Delphi Spring framework, is it possible to register a generic type with GlobalContainter? I am trying to do something like this:

TMyBaseType = class
protected
  FName: string;
  function GetName: string; virtual;
  procedure SetName(Value: string); virtual;
public
  property Name: string read GetName write SetName;
end;

TMyFirstThing = class(TMyBaseType)
protected
  function GetName: string; override;
end;

TMySecondThing = class(TMyBaseType)
protected
  procedure SetName(Value: string); override;
end;

TMyGenericType<T: TMyBaseType> = class
public
  procedure DoSomethingWithIt(AObject: T);
  function GetTheSomethingsName(AObject: T): string;
end;

// I now want to be able to use the ServiceLocator to get me an instance
// such as TMyGenericType<TMyFirstThing> or TMyGenericType<TMySecondThing>
// but I cannot figure out how to register TMyGenericType<>

......

initialization
  GlobalContainer.RegisterType<TMyGenericType<>>;
// this line fails with the messages:
// [DCC Error] E2251 Ambiguous overloaded call to 'RegisterType'
// [DCC Error] E2531 Method 'RegisterType' requires explicit type argument(s)

I'm not sure if what I'm trying to do is possible or is there a better / alternative way to do this? I am using Delphi 2010 with the latest Spring4D platform. (I also have Delphi XE5, but the project itself is still 2010 due to third-party libraries). Any ideas or suggestions would be most appreciated.

+4
source share
1 answer

Delphi does not have unrelated (or open) generic types (something like TMyGenericType<>.

(TMyGenericType<TMyFirstThing>, TMyGenericType<TMySecondThing>,...) .

#, ++ Delphi: http://blogs.teamb.com/craigstuntz/2009/10/01/38465/

+6

Source: https://habr.com/ru/post/1533451/


All Articles