--------------
建议去万一博客学习泛型
此文参考:
https://blog.csdn.net/sndayYU/article/details/77401813
如何在Delphi中将泛型类型转换为实际类型 - 程序园 http://www.voidcn.com/article/p-fnhqdxab-bwh.html
https://blog.csdn.net/henreash/article/details/7048349
--------------
------------------Unit
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TMyClass = class
private
FSomeProp: String;
function GetSomeProp: String;
class function GetProp<T>(var ADataValue: T; const ARetriever: TFunc<T>): T;
public
property SomeProp:string read GetSomeProp;
end;
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
Generics.Collections, Generics.Defaults,TypInfo;
{$R *.dfm}
Type
TFun = function: string;
TReferenceFun = reference to function: string;
function TestFun: string;
begin
Result := '函数类型对象用法';
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i:string;
begin
i:='1';
ShowMessage(TMyClass.GetProp<string>(i,TestFun));
end;
Procedure TForm1.FormCreate(Sender: TObject);
var
Fun: TFun;
RFun: TReferenceFun;
str: Integer;
begin
RFun :=function: string
begin
Result := '匿名函数reference用法';
end;
ShowMessage(RFun);
//Fun := TestFun;
//ShowMessage(Fun);
end;
class function TMyClass.GetProp<T>(var ADataValue: T; const ARetriever: TFunc<T>): T;
var
p:PTypeInfo;
aa:T;
{TTypeKind = (tkUnknown, tkInteger, tkChar, tkEnumeration, tkFloat,
tkString, tkSet, tkClass, tkMethod, tkWChar, tkLString, tkWString,
tkVariant, tkArray, tkRecord, tkInterface, tkInt64, tkDynArray, tkUString,
tkClassRef, tkPointer, tkProcedure)}
begin
p:=TypeInfo(T);
GetTypeData(p);
if p.Kind in [ tkString,tkUString] then
begin
Result:=ARetriever;
end;
end;
function TMyClass.GetSomeProp: String;
begin
Result := GetProp<String>(FSomeProp,
function: String
begin
Result := '';
end);
end;
end.
-----------------------------
procedure TForm1.CheckType<T>;
var
p: PTypeInfo;
begin
p := System.TypeInfo(T);
case p.Kind of
tkInteger, tkInt64:
begin
case GetTypeData(p).OrdType of
otSByte: ShowMessage('SByte');
otUByte: ShowMessage('UByte');
otSWord: ShowMessage('SWord');
otUWord: ShowMessage('UWord');
otSLong: ShowMessage('SLong');
otULong: ShowMessage('ULong');
end;
end;
tkFloat:
begin
case GetTypeData(p).FloatType of
ftSingle: ShowMessage('Single');
ftDouble: ShowMessage('Double');
ftExtended: ShowMessage('Extended');
ftComp : ShowMessage('Comp');
ftCurr: ShowMessage('Curr');
end;
end;
tkChar: ShowMessage('Char');
tkWChar: ShowMessage('WChar');
tkEnumeration: ShowMessage('Boolean');//枚举类型或Boolean类型,需要加以区分
end;
end;
————————————————
版权声明:本文为CSDN博主「henreash」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/henreash/article/details/7048349
---------------
-------------------Unit
--------------Form
object Form1: TForm1
Left = 823
Top = 463
Caption = 'Form1'
ClientHeight = 277
ClientWidth = 389
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
Position = poDesigned
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 160
Top = 152
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
end
---------------Form