DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: laidabin
今日帖子: 0
在线用户: 2
导航: 论坛 -> DELPHI技术 斑竹:liumazi,sephil  
作者:
男 coater (coater) ★☆☆☆☆ -
普通会员
2019/4/8 15:49:31
标题:
字符串名称转变为变量名称,可能不 浏览:1589
加入我的收藏
楼主: function nametovar(variantstring:string):string;
begin
 result:=variantstring;//比如存在一个全局变量,myvar,采用此函数nametovar('myvar'),可以输出myvar变量的值
end;

按名字调用方法可以实现:
https://www.cnblogs.com/wxc7009/p/6618509.html
那按名字调用变量可否实现?
----------------------------------------------
-
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/4/8 16:21:04
1楼: 问问 RTTI 吧。
我水平低,不敢去研究 RTTI。
----------------------------------------------
(C)(P)Flying Wang
作者:
男 sail2000 (小帆工作室) ★☆☆☆☆ -
盒子活跃会员
2019/4/8 20:17:01
2楼: 有,以前 CSDN 有人提问过一模一样的问题,有高手(好像是伴水?)解答过。
现在太反感 CSDN 了,没去很久了。
----------------------------------------------
delphi 是兴趣,和工作无关,即使它倒闭。又不靠它 delphi 吃饭,怕甚?
作者:
男 wr960204 (武稀松) ★☆☆☆☆ -
盒子活跃会员
2019/4/8 23:55:06
3楼: 对象或者结构体成员变量是可以的
独立的全局,局部变量没有RTTI信息.
----------------------------------------------
武稀松http://www.raysoftware.cn
作者:
男 coater (coater) ★☆☆☆☆ -
普通会员
2019/4/9 9:04:31
4楼: 感谢大侠们! 我想换一个思路,如果能够获取当前变量列表(集合)(问题是好像没有获取变量集合的函数),再通过变量.tostring 对比字符来判断。这样是否可行?
----------------------------------------------
-
作者:
男 xlonger (xlonger) ★☆☆☆☆ -
普通会员
2019/4/9 9:07:49
5楼: Delphi 不就是这样的软件吗?
----------------------------------------------
我打的是酱油,而不是别的什么油。
我灌的是口水,而不是别的什么水。
我聊的折腾不是那个不折腾的折腾。
我说的阿娇不是那个邓玉娇的阿娇。
3个代表,6个为什么,9个肠胃炎。
D性强的领导干部都不喜欢热比娅。
我特别要讲的是,屁民网黄色论坛是我经常上网必选的 网站之一
作者:
男 744840146 (744840146) ▲▲▲▲▲ -
普通会员
2019/4/9 9:24:41
4楼: 使用mORMot吧,这个可以直接把一个variant对象转化成JSON格式

var
  aVar: Variant;
begin
  TDocVariant.NewFast(aVar);
  aVar.Name := 'John';
  aVar.Age := 35;
  ShowMessage(TDocVariantData(aVar).u['Name']);
end;
最后这个u['Name']就是你要给的参数,如果有值就显示,没有会报错,找不到这个指定的值
----------------------------------------------
-
作者:
男 keymark (嬲) ▲▲▲△△ -
普通会员
2019/4/9 19:21:36
6楼: 谢 4楼。。。。之前就只知道加不知道解。。按此在新窗口浏览图片
----------------------------------------------
[alias]  co = clone --recurse-submodules  up = submodule update --init --recursiveupd = pullinfo = statusrest = reset --hard懒鬼提速https://www.cctry.com/>http://qalculate.github.io/downloads.htmlhttps://www.cctry.com/
作者:
男 qiuyan81 (苦恋树) ★☆☆☆☆ -
普通会员
2019/4/9 21:07:44
7楼: 没有rttl信息,对于编译器来说只有指针地址,不存在变量名.
----------------------------------------------
作者:
男 coater (coater) ★☆☆☆☆ -
普通会员
2019/4/15 21:30:23
8楼: https://theroadtodelphi.com/2012/07/06/a-quick-guide-to-evaluate-and-compile-expressions-using-the-livebindings-expression-evaluator/?unapproved=28398&moderation-hash=42d61b53d0cb83d2be69d089e51b4c1a#comment-28398

----------
The LiveBindings technology introduced in Delphi XE2, includes a set of interfaces, classes and methods to evaluate and compile expressions.
----------

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages,  System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
   System.Rtti,
  System.Bindings.EvalProtocol,
  System.Bindings.Evaluator,
  System.Bindings.EvalSys,
  System.SysUtils, Vcl.StdCtrls,
  System.Bindings.Methods,
  System.Bindings.Consts,
  System.Bindings.ObjEval;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Memo2: TMemo;
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  myvariant:string ='123';
implementation

{$R *.dfm}


procedure DoIt;
Var
  LScope : IScope;
  LCompiledExpr : ICompiledBinding;
  LResult : TValue;

begin
  LScope:= BasicOperators;
  LCompiledExpr:= Compile('Form1.Memo2.Lines.Strings[strtoint(Form1.ComboBox1.Text)]', LScope);
  LResult:=LCompiledExpr.Evaluate(LScope, nil, nil).GetValue;
  if not LResult.IsEmpty then
  Form1.Memo1.Lines.add(LResult.ToString);
end;
procedure DoIt2;
Var
  LScope : IScope;
  LCompiledExpr : ICompiledBinding;
  LResult : TValue;
  LDictionaryScope: TDictionaryScope;
begin
  LScope:= TNestedScope.Create(BasicOperators, BasicConstants);
  LDictionaryScope := TDictionaryScope.Create;
  //add a set of constants to the Scope
  LDictionaryScope.Map.Add('MinsPerHour', TValueWrapper.Create(MinsPerHour));
  LDictionaryScope.Map.Add('MinsPerDay', TValueWrapper.Create(MinsPerDay));
  LDictionaryScope.Map.Add('MSecsPerSec', TValueWrapper.Create(MSecsPerSec));
  LDictionaryScope.Map.Add('MSecsPerDay', TValueWrapper.Create(MSecsPerDay));
  LDictionaryScope.Map.Add('myvariant', TValueWrapper.Create(myvariant));
  //merge the scopes
  LScope:= TNestedScope.Create(LScope, LDictionaryScope);
    ;
  LCompiledExpr:= Compile(Form1.Memo2.Lines.Strings[strtoint(Form1.ComboBox1.Text)], LScope);
  LResult:=LCompiledExpr.Evaluate(LScope, nil, nil).GetValue;
  if not LResult.IsEmpty then
    Form1.Memo1.Lines.add(LResult.ToString);

end;

procedure DoIt3;
Var
  LScope : IScope;
  LCompiledExpr : ICompiledBinding;
  LResult : TValue;
  LDictionaryScope: TDictionaryScope;
begin
  LScope:= TNestedScope.Create(BasicOperators, BasicConstants);
  //add the registered methods
  LScope := TNestedScope.Create(LScope, TBindingMethodsFactory.GetMethodScope);
  LCompiledExpr:= Compile(Form1.Memo2.Lines.Strings[strtoint(Form1.ComboBox1.Text)], LScope);
  LResult:=LCompiledExpr.Evaluate(LScope, nil, nil).GetValue;
  if not LResult.IsEmpty then
    Form1.Memo1.Lines.add(LResult.ToString);
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
 try
   DoIt2;
 except
    on E:Exception do
        Form1.Memo1.Lines.add(E.Classname+ ':'+ E.Message);
 end;
 //Writeln('Press Enter to exit');
 //Readln;
end;

end.
----------------------------------------------
-
作者:
男 coater (coater) ★☆☆☆☆ -
普通会员
2019/4/15 21:33:20
9楼: 似乎可以实现哦
此帖子包含附件:
PNG 图像
大小:68.7K
----------------------------------------------
-
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行104.0039毫秒 RSS