我在测试项目中使用RTTI来评估枚举值,最常见的是对象的属性。如果枚举超出范围,我想显示类似于“评估/修改IDE窗口”显示的文本。类似于“((超出范围)255”)。
下面的示例代码使用TypeInfo在使用from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import NumericProperty
from kivy.lang import Builder
Builder.load_string('''
<CustLayout>:
t_length: 5
''')
class CustLayout(BoxLayout):
t_length = NumericProperty(0)
my_len = 0
print("\tCustLayout.class level-start: my_len=", my_len)
def __init__(self, **kwargs):
super(CustLayout,self).__init__(**kwargs)
print("\nCustLayout.__init__:")
self.bind(t_length=self.on_t_length)
#This statement is executed after all other prints
print("\tself.t_length=", self.t_length)
print("\tself.my_len=", self.my_len)
def on_t_length(self, instance, length):
print("\nCustLayout.on_t_length:")
#I'd like to get kv file value before the next line
self.my_len = length
print("\tself.my_len=", self.my_len)
print("\tCustLayout.class level-end: my_len=", my_len)
class TestBindProperty(App):
def build(self):
print("\nTestBindProperty.build:")
return CustLayout()
def on_stop(self):
print("\tTestBindProperty.on_stop: my_len=", self.root.my_len)
if __name__ == "__main__":
TestBindProperty().run()
时将枚举值以外的值显示为访问冲突问题。任何使用RTTI或TypeInfo的解决方案都会对我有所帮助,我只是不知道测试代码中的枚举类型
GetEnumName
答案 0 :(得分:9)
使用GetTypeData()
从PTypeInfo
获取更多详细信息,例如:
procedure WriteEnum(const ATypeInfo: PTypeInfo; const AOrdinal: Integer);
var
LTypeData: PTypeData;
begin
LTypeData := GetTypeData(ATypeInfo);
if (AOrdinal >= LTypeData.MinValue) and (AOrdinal <= LTypeData.MaxValue) then
WriteLn(Format('Ordinal: %d = "%s"', [AOrdinal, GetEnumName(ATypeInfo, AOrdinal)]))
else
WriteLn(Format('Ordinal: %d (out of bound)', [AOrdinal]));
end;