0

I'm coding a Rtti class, intended to simplify and generalize operations with Rtti:

tRTTI_Assistant = class (tObject)
 private
  fSourceObject : tObject;
 ...
  property SourceObject : tObject read fSourceObject write fSourceObject;
 ...
 end;

How can I declare a property that will receive a Record?

user2383818
  • 709
  • 1
  • 8
  • 19
  • As you can see from the example, `fSourceObject` can be any object, no matter its structure. Then, using the Rtti cappabilities, I can extract all elements of the structure. I need a similar way for passing any record structure and then use Rtti the same way I do with `fSourceObject`. – user2383818 Nov 06 '14 at 20:19
  • *How can I declare a property that will receive a Record?* You cannot. – David Heffernan Nov 06 '14 at 21:00
  • To answer this question, use a property of `TValue` instead. With RTTI and `TValue` you can resolve all information about records,classes etc. To get a `TValue` from a record, use `TValue.From(MyRecord));`. This is more or less the same question as [How to determine using Rtti, if a field from a class is a Record](http://stackoverflow.com/q/26832540/576719). – LU RD Nov 09 '14 at 22:58

2 Answers2

0

You cannot add a record class property that can take any record structure and use RTTI to resolve the inner details of the record.

For this to work you have to use another way. A TValue can be used with RTTI to resolve any type.

So declare:

tRTTI_Assistant = class (tObject)
 private
  //fSourceObject : tObject; // ! Use AnySource for objects as well
  fAnySource : TValue;
 ...
  //property SourceObject : tObject read fSourceObject write fSourceObject;
  property AnySource: TValue read fAnySource write fAnySource;
 ...
 end;

And call it:

myAssistant.AnySource := TValue.From(myRecord);

Now you can use RTTI for resolving not just record types, but any type.

See Convert Record to Serialized Form Data for sending via HTTP for examples how work with RTTI on a TValue:

Community
  • 1
  • 1
LU RD
  • 34,438
  • 5
  • 88
  • 296
-1

I'm not shure what you exactly want to do. but i think Generics are what you are looking for.

type
  TMyRecord = record
    I:Integer;
    S:string;
    B:Boolean;
  end;

  TMyObject<T:record> = class
  private
    FMyRecord:T;
  public
    property MyRecord: T read FMyRecord write FMyRecord;
  end;
linluk
  • 1,650
  • 16
  • 33
  • No, this is not what the poster is looking for. The poster is looking to get information using RTTI. – Ken White Nov 07 '14 at 21:03
  • The generic here adds nothing. And yes, asker is interested in runtime rather than compile time. – David Heffernan Nov 07 '14 at 21:32
  • But he is asking because he wants to build a `TRtti_Assistent` class (how he called it). The rtti stuff is what he want to do inside the class, he has not asked about the rtti but about how he could pass any record to a property like he could pass any object to a `TObject` property. With the generic class he could pass any record he want and do the rtti stuff on it. – linluk Nov 08 '14 at 08:48
  • You have to understand the difference between compile time and run time typing. Generics require the caller to know the types at compile time. Your generic type serves no purpose. You may as well just use the record directly. – David Heffernan Nov 08 '14 at 09:47
  • i know the difference between run- and compile time typing. lets assume he want to write a simple editor for his record values using rtti he could write the class using rtti. but he has to pass his record to the class which does the editing (with the rtti stuff). For example `XSuperObject` also combines generics and rtti. in superobject you have a generic function where you can pass your record and it does the rtti stuff to generate the json string. His Question was: `How can I declare a property that will receive a Record?` only the questions title is about rtti. – linluk Nov 08 '14 at 10:25