/
reflections.pas
761 lines (701 loc) · 23.2 KB
/
reflections.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
unit reflections;
interface
uses
System.Classes,
System.SysUtils,
System.StrUtils,
System.TypInfo,
System.Generics.Defaults,
System.Generics.Collections,
RTTI;
type
TPropertyInfo = record
PropType: string;
PropTypeQualifiedName: string;
PropLength: integer;
TypeKind: System.TypInfo.TTypeKind;
end;
TPropertyInfoList = TDictionary<string, TPropertyInfo>;
TPropertyValueList = TDictionary<string, Variant>;
TReflection = class
public
class function GetProperties(AClass: TClass): TPropertyInfoList;
class procedure SetPropertyValue(AObject: TObject;
const APropertyName: string; const AValue: Variant);
class function GetObjectProperties(AObject: TObject; AClass: TClass = nil): TPropertyValueList;
class function GetObjectPropertiesAsString(AObject: TObject; AClass: TClass = nil; convertToUnderscore: boolean = true): string;
class function GetPropertyValue(AObject: TObject; const APropertyName: string): Variant;
class function GetPropertyObject(AObject: TObject; const APropertyName: string): TObject;
class function GetEnumNames(AClass: TClass; APropertyName: string): TStringList; overload;
class function GetEnumNames(ATypeInfo: PTypeInfo): TStringList; overload;
class function GetTypeByName(AName: string): TRttiType;
class function EnumToString(ATypeInfo: pointer; enumVal: integer): string;
class function StringToEnum(ATypeInfo: pointer; enumName: string): integer;
class function TryGetProperty(AObject: TObject; const AName: string; out AProperty: TRttiProperty): boolean; overload;
class function TryGetProperty(ARttiType: TRttiType; const AName: string; out AProperty: TRttiProperty): boolean; overload;
class function PropertyExists(ARttiType: TRttiType; const AName: string): boolean; overload;
class function PropertyExists(AObject: TObject; const AName: string): boolean; overload;
class function TryGetMethod(AObject: TObject; const AName: string; out AMethod: TRttiMethod): boolean; overload;
class function TryGetMethod(ARttiType: TRttiType; const AName: string; out AMethod: TRttiMethod): boolean; overload;
class function GetRttiType(AObject: TObject): TRttiType;
class function TryGetField(AObject: TObject; const AName: string; out AField: TRttiField): boolean; overload;
class function TryGetField(ARttiType: TRttiType; const AName: string; out AField: TRttiField): boolean; overload;
class function GetDefaultVariant(AClassName: string ; APropertyName: string): Variant;
class function GetObjectListItemClass(AObject: TObject): string;
class function ContainsPropertyOrField(AObject: TObject; AName: string): boolean;
class function GetSimpleConstructor(val: TRttiInstanceType; raiseExceptionIfNotFound: boolean = true): TRttiMethod;
class function tryGetSimpleConstructor(val: TRttiInstanceType; out constructorMethod: TRttiMethod): Boolean;
class function InvokeObjectListConstructor(val: TRttiInstanceType; ownsObjects: boolean): TObject;
class function InvokeObjectDictionaryConstructor(val: TRttiInstanceType; Ownerships: TDictionaryOwnerships): TObject;
end;
TValueHelper = record helper for TValue
private
function GetRttiType: TRttiType;
public
function AsPointer: Pointer;
function ToObject: TObject;
function IsInterface: Boolean;
property RttiType: TRttiType read GetRttiType;
function IsCurrency: Boolean;
function IsDate: Boolean;
function IsDateTime: Boolean;
function IsTime: Boolean;
end;
implementation
uses
variants;
var
Context: TRttiContext;
class function TReflection.GetEnumNames(AClass: TClass;
APropertyName: string): TStringList;
var
P: TRttiProperty;
i, enCount: integer;
begin
Result := TStringList.Create;
P := Context.GetType(AClass).GetProperty(APropertyName);
enCount := GetTypeData(P.PropertyType.Handle).MaxValue;
for i := 0 to enCount do
begin
Result.Add(GetEnumName(P.PropertyType.Handle, i));
end;
end;
class function TReflection.ContainsPropertyOrField(AObject: TObject;
AName: string): boolean;
var
rttiType: TrttiType;
rttiField: TrttiField;
rttiProperty: TRttiProperty;
begin
rttiType := Context.GetType(AObject.ClassType);
rttiField := rttiType.GetField(AName);
rttiProperty := rttiType.GetProperty(AName);
Result := Assigned(rttiField) or Assigned(rttiProperty);
end;
class function TReflection.EnumToString(ATypeInfo: pointer; enumVal: integer): string;
begin
try
Result := GetEnumName(ATypeInfo, enumVal);
except
Result := '';
end;
end;
class function TReflection.GetDefaultVariant(AClassName,
APropertyName: string): Variant;
var
rttiType: TrttiType;
rttiField: TrttiField;
rttiProperty: TRttiProperty;
val: tValue;
begin
result := null;
rttiType := GetTypeByName(AClassName);
if rttiType = nil then
exit;
rttiField := rttiType.GetField(APropertyName);
rttiProperty := rttiType.GetProperty(APropertyName);
if rttiField <> nil then
TValue.Make(nil, rttiField.FieldType.Handle, val)
else if rttiProperty <> nil then
TValue.Make(nil, rttiProperty.PropertyType.Handle, val);
result := val.AsVariant;
end;
class function TReflection.GetEnumNames(ATypeInfo: PTypeInfo): TStringList;
var
i, enCount: integer;
begin
Result := TStringList.Create;
enCount := GetTypeData(ATypeInfo).MaxValue;
for i := 0 to enCount do
begin
Result.Add(GetEnumName(ATypeInfo, i));
end;
end;
class function TReflection.GetObjectListItemClass(AObject: TObject): string;
var
LMethod: TRttiMethod;
begin
if{ TReflection.PropertyExists(aObject, 'OwnsObject') and }TReflection.TryGetMethod(aObject, 'Add', LMethod) then
begin
Result := LMethod.GetParameters[0].ParamType.asInstance.MetaclassType.QualifiedClassName;
// LType := LMethod.GetParameters[0].ParamType.as;
// &type := LType.AsInstance.MetaclassType;
// repeat
// listItem := &type.Create;
// aFrom := mORMot.JsonToObject(listItem, aFrom, aValid, &type);
// LMethod.Invoke(aObject, [TValue.From(listItem)]);
// until (aFrom = nil) or (aFrom^ = ']');
// exit;
end;
end;
class function TReflection.GetObjectProperties(AObject: TObject; AClass: TClass)
: TPropertyValueList;
var
P: TRttiProperty;
F: TRttiField;
enumStr: String;
props: TArray<TRttiProperty>;
fields: TArray<TRttiField>;
begin
Result := TPropertyValueList.Create;
if AClass <> nil then
begin
props := Context.GetType(AClass).GetProperties;
fields := Context.GetType(AClass).GetFields;
end else
begin
props := Context.GetType(AObject.ClassType).GetProperties;
fields := Context.GetType(AObject.ClassType).GetFields
end;
for P in props do
begin
if P.Visibility >= mvPublic then
begin
if P.PropertyType.TypeKind = tkClass then
continue
else if P.PropertyType.TypeKind <> tkEnumeration then
try
Result.Add(P.Name, P.GetValue(pointer(AObject)).AsVariant)
except
{$IFDEF DEBUG}
raise Exception.Create('TReflection.GetObjectProperties: P.Name['+P.Name+'] AObject.ClassType['+AObject.ClassType.ClassName+']');
{$ELSE DEBUG}
raise
{$ENDIF}
end
else
begin
try
enumStr := P.GetValue(pointer(AObject)).ToString;
Result.Add(P.Name, GetEnumValue(P.PropertyType.Handle, enumStr));
except
{$IFDEF DEBUG}
raise Exception.Create('TReflection.GetObjectProperties: P.Name['+P.Name+'] AObject.ClassType['+AObject.ClassType.ClassName+']');
{$ELSE DEBUG}
raise
{$ENDIF}
end;
end;
end;
end;
for F in fields do
begin
if F.Visibility >= mvPublic then
begin
if F.FieldType.TypeKind = tkClass then
continue
else if F.FieldType.TypeKind <> tkEnumeration then
try
Result.Add(F.Name, F.GetValue(pointer(AObject)).AsVariant)
except
{$IFDEF DEBUG}
raise Exception.Create('TReflection.GetObjectProperties: F.Name['+F.Name+'] AObject.ClassType['+AObject.ClassType.ClassName+']');
{$ELSE DEBUG}
raise
{$ENDIF}
end
else
begin
try
enumStr := F.GetValue(pointer(AObject)).ToString;
Result.Add(F.Name, GetEnumValue(F.FieldType.Handle, enumStr));
except
{$IFDEF DEBUG}
raise Exception.Create('TReflection.GetObjectProperties: F.Name['+F.Name+'] AObject.ClassType['+AObject.ClassType.ClassName+']');
{$ELSE DEBUG}
raise
{$ENDIF}
end;
end;
end;
end;
end;
class function TReflection.GetObjectPropertiesAsString(AObject: TObject; AClass: TClass = nil; convertToUnderscore: boolean = true): string;
var
tmp: TPropertyValueList;
skey, key: string;
v: variant;
begin
Result := '';
tmp := GetObjectProperties(AObject, AClass);
if tmp = nil then
exit;
for key in tmp.Keys do
begin
v := tmp.Items[key];
skey := key;
result := result + skey + '=' + VarToStr(v) + ';';
end;
end;
class function TReflection.GetProperties(AClass: TClass): TPropertyInfoList;
var
P: TRttiProperty;
F: TRttiField;
LPropertyInfo: TPropertyInfo;
sTypeName: string;
begin
Result := TPropertyInfoList.Create;
for P in Context.GetType(AClass).GetProperties do
begin
if P.Visibility >= mvPublic then
begin
try
LPropertyInfo.PropLength := P.PropertyType.TypeSize;
LPropertyInfo.PropType := P.PropertyType.Name;
LPropertyInfo.PropTypeQualifiedName := P.PropertyType.QualifiedName;
LPropertyInfo.TypeKind := P.PropertyType.TypeKind;
Result.Add(P.Name, LPropertyInfo);
except
{$IFDEF DEBUG}
if P.PropertyType = nil then
sTypeName := 'nil'
else
sTypeName := P.PropertyType.Name;
raise Exception.CreateFmt('TReflection.GetProperties: P.Name=%s, P.PropertyType=%s, AClass.ClassType=%s', [P.Name, sTypeName, AClass.ClassName]);
{$ELSE DEBUG}
raise
{$ENDIF}
end;
end;
end;
for F in Context.GetType(AClass).GetFields do
begin
if F.Visibility >= mvPublic then
begin
try
LPropertyInfo.PropLength := F.FieldType.TypeSize;
LPropertyInfo.PropType := F.FieldType.Name;
LPropertyInfo.PropTypeQualifiedName := F.FieldType.QualifiedName;
LPropertyInfo.TypeKind := F.FieldType.TypeKind;
Result.Add(F.Name, LPropertyInfo);
except
{$IFDEF DEBUG}
if f.FieldType = nil then
sTypeName := 'nil'
else
sTypeName := F.FieldType.Name;
raise Exception.CreateFmt('TReflection.GetProperties: F.Name=%s, F.FieldType=%s, AClass.ClassType=%s', [F.Name, sTypeName, AClass.ClassName]);
{$ELSE DEBUG}
raise
{$ENDIF}
end;
end;
end;
end;
class function TReflection.GetPropertyObject(AObject: TObject;
const APropertyName: string): TObject;
var
prop: TRttiProperty;
field: TRttiField;
val: TValue;
begin
Result := nil;
prop := Context.GetType(AObject.ClassType).GetProperty(APropertyName);
if prop <> nil then
begin
if prop.PropertyType.TypeKind = tkClass then
Result := prop.GetValue(pointer(AObject)).AsObject;
end
else
begin
field := Context.GetType(AObject.ClassType).GetField(APropertyName);
if (field <> nil) and (field.FieldType.TypeKind = tkClass) then
begin
Result := field.GetValue(pointer(AObject)).AsObject;
end;
end;
end;
class function TReflection.GetPropertyValue(AObject: TObject;
const APropertyName: string): Variant;
var
P: TRttiProperty;
enumStr: String;
rec: TRttiRecordType;
field: TRttiField;
val: TValue;
begin
Result := 0;
P := Context.GetType(AObject.ClassType).GetProperty(APropertyName);
if P <> nil then
begin
case P.PropertyType.TypeKind of
System.TypInfo.TTypeKind.tkRecord:
begin
val := P.GetValue(pointer(AObject));
rec := Context.GetType(P.GetValue(pointer(AObject)).TypeInfo).AsRecord;
for field in rec.GetFields do
begin
Result := field.GetValue(val.GetReferenceToRawData).ToString;
end;
end;
System.TypInfo.TTypeKind.tkEnumeration:
begin
enumStr := P.GetValue(pointer(AObject)).ToString;
Result := GetEnumValue(P.PropertyType.Handle, enumStr);
end;
else
begin
Result := P.GetValue(pointer(AObject)).AsVariant;
end;
end;
end
else
begin
field := Context.GetType(AObject.ClassType).GetField(APropertyName);
if field <> nil then
begin
case field.FieldType.TypeKind of
System.TypInfo.TTypeKind.tkRecord:
begin
val := field.GetValue(pointer(AObject));
rec := Context.GetType(field.GetValue(pointer(AObject)).TypeInfo).AsRecord;
for field in rec.GetFields do
begin
Result := field.GetValue(val.GetReferenceToRawData).ToString;
end;
end;
System.TypInfo.TTypeKind.tkEnumeration:
begin
enumStr := field.GetValue(pointer(AObject)).ToString;
Result := GetEnumValue(field.FieldType.Handle, enumStr);
end;
else
Result := field.GetValue(pointer(AObject)).AsVariant;
end;
end;
end;
end;
class function TReflection.GetRttiType(AObject: TObject): TRttiType;
begin
Result := Context.GetType(AObject.ClassType);
end;
class function TReflection.GetSimpleConstructor(val: TRttiInstanceType; raiseExceptionIfNotFound: boolean = true): TRttiMethod;
var
i, j: integer;
method: TRttiMethod;
parmeter: TRttiParameter;
useTObjectContructor: boolean;
begin
Result := nil;
i := 1;
useTObjectContructor := true;
// UcLog.Add(100, 0, 'constructor for: ' + val.MetaclassType.ClassName);
try
for method in val.GetDeclaredMethods do
begin
if not method.IsConstructor then
continue;
// useTObjectContructor := false;
j := 1;
// UcLog.Add(100, 0, 'constructor nr: ' + IntToStr(i));
for parmeter in method.GetParameters do
begin
// UcLog.Add(100, 0,' ' + parmeter.Name + ': ' + parmeter.ParamType.QualifiedName);
inc(j);
end;
if (method.IsConstructor) and (length(method.GetParameters) = 0) then
exit(method);
inc(i)
end;
if useTObjectContructor then
begin
for method in val.GetMethods('Create') do
begin
if (method.IsConstructor) and (length(method.GetParameters) = 0) then
exit(method);
end;
end;
finally
if (Result = nil) and raiseExceptionIfNotFound then
raise EInsufficientRTTI.CreateFmt('No simple constructor available for class %s ',
[val.MetaclassType.ClassName]);
end;
end;
class function TReflection.GetTypeByName(AName: string): TRttiType;
begin
Result := Context.FindType(AName);
end;
class function TReflection.InvokeObjectDictionaryConstructor(
val: TRttiInstanceType; Ownerships: TDictionaryOwnerships): TObject;
var
method: TRttiMethod;
parameters: TArray<TRttiParameter>;
begin
result := nil;
for method in val.GetMethods('Create') do
begin
if (method.IsConstructor) then
begin
parameters := method.GetParameters;
if (length(parameters) = 2) and (parameters[0].Name = 'Ownerships') and (parameters[1].Name = 'ACapacity') then
begin
//TODO: zrobiæ TValue z ownerShips
// Result := method.Invoke(val.MetaclassType, [doOwnsKeys, 0]).AsObject;
break;
end;
end;
end;
end;
class function TReflection.InvokeObjectListConstructor(val: TRttiInstanceType; ownsObjects: boolean): TObject;
var
method: TRttiMethod;
parameters: TArray<TRttiParameter>;
begin
result := nil;
for method in val.GetMethods('Create') do
begin
if (method.IsConstructor) then
begin
parameters := method.GetParameters;
if (length(parameters) = 1) and (parameters[0].Name = 'AOwnsObjects') then
begin
Result := method.Invoke(val.MetaclassType, [ownsObjects]).AsObject;
break;
end;
end;
end;
end;
class function TReflection.PropertyExists(AObject: TObject; const AName: string): Boolean;
begin
if not Assigned(AObject) then
begin
Result := false;
exit;
end;
result := Context.GetType(AObject.ClassType).GetProperty(AName) <> nil;
end;
class function TReflection.PropertyExists(ARttiType: TRttiType; const AName: string): Boolean;
begin
result := ARttiType.GetProperty(AName) <> nil;
end;
class procedure TReflection.SetPropertyValue(AObject: TObject;
const APropertyName: string; const AValue: Variant);
var
P: TRttiProperty;
F: TRttiField;
AVal: TValue;
debugValue, strVal: string;
lDate: tDateTime;
varT: word;
lOdometerReq: boolean;
enumVal: integer;
begin
AVal := TValue.FromVariant(AValue);
P := Context.GetType(AObject.ClassType).GetProperty(APropertyName);
if P <> nil then
begin
if not P.IsWritable then
exit;
try
if P.PropertyType.TypeKind = tkEnumeration then
begin
if VarIsStr(AValue) then
begin
enumVal := GetEnumValue(P.PropertyType.Handle, AValue);
TValue.Make(enumVal, P.PropertyType.Handle, AVal);
end
else
TValue.Make(AValue, P.PropertyType.Handle, AVal);
end
else if (P.PropertyType.TypeKind = tkString) or (P.PropertyType.TypeKind = tkUString) then
begin
if VarIsNull(AValue) then
strVal := ''
else
strVal := trim(AValue);
AVal := TValue.From(strVal);
end ;
P.SetValue(pointer(AObject), AVal);
except
on e: exception do
begin
if VarIsNull(AValue) then
debugValue := 'Null'
else
debugValue := AValue;
raise Exception.CreateFmt('Could not assign value: %s to property %s.%s of type %s',
[debugValue, AObject.ClassName, APropertyName, P.PropertyType.ToString] );
end;
end;
end
else
begin
F := Context.GetType(AObject.ClassType).GetField(APropertyName);
if F <> nil then
begin
try
if F.FieldType.TypeKind = tkEnumeration then
begin
if VarIsStr(AValue) then
begin
enumVal := GetEnumValue(F.FieldType.Handle, AValue);
TValue.Make(enumVal, F.FieldType.Handle, AVal);
end
else
TValue.Make(AValue, F.FieldType.Handle, AVal);
end
else if (F.FieldType.TypeKind = tkString) or (F.FieldType.TypeKind = tkUString) then
begin
if VarIsNull(AValue) then
strVal := ''
else
strVal := trim(AValue);
AVal := TValue.From(strVal);
end
else if (F.FieldType.TypeKind = tkInteger) and VarIsType(AValue,varBoolean) then
begin
lOdometerReq := VarAsType(AValue, varBoolean);
AVal := TValue.FromVariant(Ord(lOdometerReq));
end
else if (F.FieldType.TypeKind = tkFloat) then
begin
varT := VarType(AValue);
if (VarT = varUString) or (varT = varString) then
begin
lDate := StrToDateTime(AValue);
AVal := TValue.FromVariant(VarFromDateTime(lDate));
end;
end;
// F.FieldType.TypeKind = tk
// AVal := TValue.FromVariant((VarAsType(AValue)))
F.SetValue(pointer(AObject), AVal);
except
on e: exception do
begin
if VarIsNull(AValue) then
debugValue := 'Null'
else
debugValue := AValue;
raise Exception.CreateFmt('Could not assign value: %s to field %s.%s of type %s',
[debugValue, AObject.ClassName, APropertyName, F.FieldType.ToString] );
end;
end;
end;
end;
end;
class function TReflection.StringToEnum(ATypeInfo: pointer;
enumName: string): integer;
begin
Result := GetEnumValue(ATypeInfo, enumName);
end;
class function TReflection.TryGetMethod(AObject: TObject; const AName: string;
out AMethod: TRttiMethod): Boolean;
begin
if not Assigned(AObject) then
begin
Result := false;
exit;
end;
AMethod := Context.GetType(AObject.ClassType).GetMethod(AName);
Result := Assigned(AMethod);
end;
class function TReflection.TryGetField(AObject: TObject; const AName: string;
out AField: TRttiField): Boolean;
begin
if not Assigned(AObject) then
begin
Result := false;
exit;
end;
AField := Context.GetType(AObject.ClassType).GetField(AName);
Result := Assigned(AField);
end;
class function TReflection.TryGetField(ARttiType: TRttiType;
const AName: string; out AField: TRttiField): Boolean;
begin
AField := ARttiType.GetField(AName);
Result := Assigned(AField);
end;
class function TReflection.TryGetMethod(ARttiType: TRttiType;
const AName: string; out AMethod: TRttiMethod): Boolean;
begin
AMethod := ARttiType.GetMethod(AName);
Result := Assigned(AMethod);
end;
class function TReflection.TryGetProperty(ARttiType: TRttiType;
const AName: string; out AProperty: TRttiProperty): Boolean;
begin
AProperty := ARttiType.GetProperty(AName);
Result := Assigned(AProperty);
end;
class function TReflection.tryGetSimpleConstructor(val: TRttiInstanceType; out constructorMethod: TRttiMethod): boolean;
begin
constructorMethod := GetSimpleConstructor(val, false);
Result := Assigned(constructorMethod);
end;
class function TReflection.TryGetProperty(AObject: TObject; const AName: string;
out AProperty: TRttiProperty): Boolean;
begin
if not Assigned(AObject) then
begin
Result := false;
exit;
end;
AProperty := Context.GetType(AObject.ClassType).GetProperty(AName);
Result := Assigned(AProperty);
end;
{ TValueHelper }
function TValueHelper.AsPointer: Pointer;
begin
if Kind in [tkClass, tkInterface] then
Result := ToObject
else
Result := GetReferenceToRawData;
end;
function TValueHelper.GetRttiType: TRttiType;
begin
Result := Context.GetType(TypeInfo);
end;
function TValueHelper.IsCurrency: Boolean;
begin
Result := false;
//TODO: dokonczyc
end;
function TValueHelper.IsDate: Boolean;
begin
Result := Assigned(TypeInfo) and (TypeInfo.Kind = tkFloat);
end;
function TValueHelper.IsDateTime: Boolean;
begin
Result := Assigned(TypeInfo) and (TypeInfo.Kind = tkFloat);
end;
function TValueHelper.IsInterface: Boolean;
begin
Result := Assigned(TypeInfo) and (TypeInfo.Kind = tkInterface);
end;
function TValueHelper.IsTime: Boolean;
begin
Result := Assigned(TypeInfo) and (TypeInfo.Kind = tkFloat); //TODO: dokonczyc
end;
function TValueHelper.ToObject: TObject;
begin
if IsInterface then
Result := AsInterface as TObject
else
Result := AsObject;
end;
initialization
Context := TRttiContext.Create;
finalization
Context.Free;
end.