Fórum attributos, generics #499758
31/10/2014
0
vamos debater...
temos o seguinte codigo...
getAttributeProperty retorna uma string com o conteudo da propriedade do objeto passado...
o valor da propriedade é retornado por uma metodo anonimo...
o problema...
como tratar diversos tipos de dados ?!
neste caso so funciona com string...
mas se fosse um campo por exemplo interger, date ?!?!
como adaptar essas rotinas para retornar um valor de acordo com o tipo do campo ?!
valeeeeu !!!!
class function TTeste.getAttributeProperty<T>(pObject : T; ptx : tx) : string;
var
vObject : TRttiContext;
vObjectType : TRttiType;
vAttribute : TCustomAttribute;
s : string;
begin
s:= '';
vObject:= TRttiContext.Create;
vObjectType:= vObject.GetType(TObject(pObject).ClassInfo);
for vAttribute in vObjectType.GetAttributes do
begin
s:= ptx(vAttribute);
if s <> '' then
Exit(s);
end;
end;
class function TTeste.getName<T>(pObject : T) : string;
begin
result:= getAttributeProperty(pObject,function (t : tobject) : string
begin
if t is TPessoa then
result:= TPessoa(t).Name;
end);
end;
temos o seguinte codigo...
getAttributeProperty retorna uma string com o conteudo da propriedade do objeto passado...
o valor da propriedade é retornado por uma metodo anonimo...
o problema...
como tratar diversos tipos de dados ?!
neste caso so funciona com string...
mas se fosse um campo por exemplo interger, date ?!?!
como adaptar essas rotinas para retornar um valor de acordo com o tipo do campo ?!
valeeeeu !!!!
class function TTeste.getAttributeProperty<T>(pObject : T; ptx : tx) : string;
var
vObject : TRttiContext;
vObjectType : TRttiType;
vAttribute : TCustomAttribute;
s : string;
begin
s:= '';
vObject:= TRttiContext.Create;
vObjectType:= vObject.GetType(TObject(pObject).ClassInfo);
for vAttribute in vObjectType.GetAttributes do
begin
s:= ptx(vAttribute);
if s <> '' then
Exit(s);
end;
end;
class function TTeste.getName<T>(pObject : T) : string;
begin
result:= getAttributeProperty(pObject,function (t : tobject) : string
begin
if t is TPessoa then
result:= TPessoa(t).Name;
end);
end;
Felipe Barros
Curtir tópico
+ 0
Responder
Posts
03/11/2014
Renato Rubinho
Buenas,
Retorna com tipo variant.
class function TTeste.getAttributeProperty<T>(pObject : T; ptx : tx) : variant;
Abraççç,
Renato
Retorna com tipo variant.
class function TTeste.getAttributeProperty<T>(pObject : T; ptx : tx) : variant;
Abraççç,
Renato
Responder
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)