数组键名和文件条目的更好关联是什么?
资料来源:
// write
Path := 'Data/_prefixed' + dynarray1[v].Name;
with XMLConfig1 do begin;
SetValue(Path + '/RealName', dynarray1[v].Name);
end;
..
// read
Path := 'Data/_prefixed' + listOfVars.Strings[v];
with XMLConfig1 do begin;
dynarray1[v].Name := GetValue(Path + '/RealName', '');
end;
我考虑编写简单的宏,例如:
procedure getsetkey(Keyname,Var:string; var Value: **overloaded**; Direction: integer);
getset('Data/_prefixed'+Keyname, Var
但由于返回类型不同,它仍然是三个(整数、字符串、布尔)类似的函数(没有什么像?),或者是六个函数,如果我抛出 Direction 参数并得到一个集合setkey
和 getkey
,或 key(重载:如果存在第 4 个参数则调用 set)
,(值允许为空,因此检查值不是决定的方法)
有什么方法可以缩短函数数量并将 dynarray1[v].Element_Name 与可以传递给 GetValue() 的键相关联?某种类似反射的 API 来访问键名?
Source:
// write
Path := 'Data/_prefixed' + dynarray1[v].Name;
with XMLConfig1 do begin;
SetValue(Path + '/RealName', dynarray1[v].Name);
end;
..
// read
Path := 'Data/_prefixed' + listOfVars.Strings[v];
with XMLConfig1 do begin;
dynarray1[v].Name := GetValue(Path + '/RealName', '');
end;
I think about writing simple macro like:
procedure getsetkey(Keyname,Var:string; var Value: **overloaded**; Direction: integer);
getset('Data/_prefixed'+Keyname, Var
but due to different return types it is still three (integer, string, boolean) similar functions (nothing like ?), or ever six function, if I throw out Direction argument and get a sets of setkey
and getkey
, or key(overload: call set if 4-th parameter presents)
, (Value is allowed to be empty, so checking the value isn't the way to decide)
Any way to shorten number of functions and to associate dynarray1[v].Element_Name with a key that may be passed to GetValue()? Some kind of Reflection-like API to access key names?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
变体会更合乎逻辑。如果您可以获得反射/RTTI 信息(例如您想要保存的所有内容都是已发布的属性),那也是可能的。
但恕我直言,对于 1 行函数的 3 种变体,就像用佳能在蚊子上射击一样
Variants would be more logical. If you can get reflection/RTTI info (e.g. everything you want to save is a published property) that is also possible.
But IMHO for 3 variants of a 1 line function that is like shooting with a canon on a mosquito