Delphi Spring框架注册泛型类型(Delphi Spring framework register generic type)

系统教程 行业动态 更新时间:2024-06-14 16:57:40
Delphi Spring框架注册泛型类型(Delphi Spring framework register generic type)

使用Delphi Spring框架,是否可以使用GlobalContainter注册泛型类型? 我正在尝试做这样的事情:

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)

我不确定我尝试做的是否可行,或者是否有更好的/替代方法可以做到这一点? 我正在使用Delphi 2010和最新的Spring4D框架。 (我也有Delphi XE5,但由于第三方库,该项目本身仍然是2010年)。 任何想法或建议将非常感激。

Using the Delphi Spring framework, is it possible to register a generic type with the GlobalContainter? I'm 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 if there's a better/alternative way to do it? I'm using Delphi 2010 with the latest Spring4D framework. (I also have Delphi XE5 but the project itself is still 2010 due to 3rd party libraries). Any ideas or suggestions would be most appreciated.

最满意答案

Delphi没有未绑定(或开放)泛型类型(类似于TMyGenericType<> 。

在您的情况下,您必须TMyGenericType<TMyFirstThing>注册每个封闭的构造泛型类型( TMyGenericType<TMyFirstThing> , TMyGenericType<TMySecondThing> ,...)。

关于C#,C ++和Delphi中泛型差异的更多信息: http : //blogs.teamb.com/craigstuntz/2009/10/01/38465/

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

In your case you have to register every closed constructed generic type (TMyGenericType<TMyFirstThing>, TMyGenericType<TMySecondThing>, ...) individually.

More informations about the differences of generics in C#, C++ and Delphi: http://blogs.teamb.com/craigstuntz/2009/10/01/38465/

更多推荐

本文发布于:2023-04-13 12:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/4047e789b40762f90a8921c069fddfdf.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框架   类型   Spring   Delphi   type

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!