首页 新闻 会员 周边

delphi中如何利用反射机制给枚举类型的属性赋值?

1
悬赏园豆:10 [已关闭问题] 关闭于 2010-09-16 18:22

delphi2010中提供了反射机制,万一大侠的例子中给出了利用反射机制给属性赋值的代码。不过所给代码中属性的类型是基本类型(整型、字符串), 但是,如果换成枚举类型问题就不好办了!我尝试了多次没有成功!

期待各位大侠的指点!!

问题补充: 问题已经解决!下面是源代码。 procedure TConfig.serialize(propertyName: string; saveIt: Boolean); var tmplist: TStringList; ctx: TRttiContext; t: TRttiType; p: TRttiProperty; v: TValue; i, N, k: Integer; tt: TTypeKind; o: TObject; aPropInfo: PPropInfo; itemName: string; // aInteger: Integer; aFloat: Double; aString: string; aChar: Char; aV: TValue; begin if (iniFile = nil) or (obj = nil) or (sectionName = '') then begin raise Exception.Create('请给iniFile or O or sectionName 赋值!'); end; o := obj; tmplist := TStringList.Create; tmplist.Delimiter := '.'; try tmplist.DelimitedText := Trim(propertyName); n := tmplist.Count; for i := 0 to N - 1 do begin itemName := tmplist[i]; t := ctx.GetType(o.ClassType); p := t.GetProperty(itemName); if p<>nil then begin v := p.GetValue(o); if v.IsObject then o := v.AsObject else begin if saveit then begin iniFile.WriteString(sectionName, propertyName + '.type', Format('%d',[Ord(v.TypeInfo.Kind)])); iniFile.WriteString(sectionName, propertyName, v.ToString); end else begin k := iniFile.ReadInteger(sectionName, propertyName + '.type', 1); tt := TTypeKind(k); case tt of tkUnknown: begin ShowMessage(propertyName + ':不支持!' + Format(':%d',[k])); end; tkInteger: begin aInteger := iniFile.ReadInteger(sectionName, propertyName, 0); if Pos('Count', propertyName) = 0 then begin p.SetValue(o, aInteger) end else b
菜鸟在努力的主页 菜鸟在努力 | 初学一级 | 园豆:200
提问于:2010-09-08 09:56
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册