我确实知道如何将枚举属性作为字符串写入:
变量 形式:TForm; LContext:TRttiContext; LType:TRttiType; LProperty:TRttiProperty; PropTypeInfo:PTypeInfo; 值:TValue; 开始 表格 := TForm.Create(NIL); LContext := TRttiContext.Create; LType := LContext.GetType(Form.ClassType); 对于 LType.GetProperties 中的 LProperty 做 如果 LProperty.Name = 'FormStyle' 那么 开始 PropTypeInfo := LProperty.PropertyType.Handle; TValue.Make(GetEnumValue(PropTypeInfo, 'fsStayOnTop'), PropTypeInfo, Value); LProperty.SetValue(Form, Value); 结尾; writeln(整数(Form.FormStyle)); // = 3
但是如果我没有字符串而是整数(例如,fsStayOnTop 为 3),如何设置值,以及如何从该属性中读取但不返回字符串(与 Value.AsString 一起使用)?
值 := LProperty.GetValue(Obj); writeln(Value.AsString); // 返回 fsStayOnTop 但我不想要一个字符串,我想要一个整数 writeln(Value.AsInteger); // 失败