I am creating a control similar to object inspector , So i want to assign any changes to the property to the relevant object.
var
v:TValue ;
ctx : TRttiContext;
begin
// k.IsOrdinal := true ;
v := v.FromVariant(2) ;
ctx.GetType(tButton).GetProperty('Style').SetValue(Button1, v.AsOrdinal);
end;
above is my code , but i am getting invalid type cast error.
Is it possible to handle any variable and enums .(No need of objects and records as it is very complicated )
The call to SetValue needs to read like this:
In your code, the use of
AsOrdinalis incorrect. That is a function that returns aTRttiOrdinalType. ButTRttiOrdinalTypeis described thus:But you need to provide a
TValuethat represents aTButtonStyle, which is what the code above achieves.As an aside, I initially tried to use the generic
TValue.From<T>()function like this:But that just resulted in the following internal compiler error:
QC#103129
Every time I attempt to use generics I end up being defeated by these internal errors!
Thanks to Serg for pointing out the alternative form of calling the parameterised method using type inference does not fall foul of the internal error.