C#中可以动态定义变量名吗?

chu715322834 2010-04-08 11:28:02
假设 从文本框获得一个值
string name = txtName.text;
然后定义了个泛型集合
Dictionary<string,BodyCheckItem> "这里可以用name做动态变量名吗"
= new Dictionary<string,BodyCheckItem>();
...全文
610 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
玩命三少 2011-09-22
  • 打赏
  • 举报
回复
拿分,走人!
xt218218 2010-04-08
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lost_painting 的回复:]

可以.
动态生成类
参考这里:http://dotnet.chinaitlab.com/CSharp/745310.html
[/Quote]

动态可以 生成一个变量的
鸭梨山大帝 2010-04-08
  • 打赏
  • 举报
回复
可以.
动态生成类
参考这里:http://dotnet.chinaitlab.com/CSharp/745310.html
zzxap 2010-04-08
  • 打赏
  • 举报
回复
int getValue(string wl)
{
FieldInfo fi=this.GetType().GetField("a"+wl
,BindingFlags.NonPublic|BindingFlags.Instance
);
if(fi==null)
throw(new Exception("cant find a"+wl));
if(fi.FieldType!=typeof(int))
throw(new Exception("type unmatched"));
return (int)fi.GetValue(this);
}
using System;
using System.Reflection;

namespace XX
{
class Class1
{
int bc=100;
int abc=1000;
public int getValue(string wl)
{
FieldInfo fi=this.GetType().GetField("a"+wl
,BindingFlags.NonPublic|BindingFlags.Instance
);
if(fi==null)
throw(new Exception("cant find a"+wl));
if(fi.FieldType!=typeof(int))
throw(new Exception("type unmatched"));
return (int)fi.GetValue(this);
}
public void Run()
{
Console.Write(this.getValue("bc"));
}
}
class XX
{
[STAThread]
static void Main(string[] args)
{
new Class1().Run();
}
}
}
wozaiqingniao 2010-04-08
  • 打赏
  • 举报
回复
跑偏了,我看成了,把那么放到里面去了,要是做那个的变量名,的确不能!
chu715322834 2010-04-08
  • 打赏
  • 举报
回复
- -|||
chu715322834 2010-04-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wozaiqingniao 的回复:]
可以。你把name放进去的时候,比如说:第一次你放进去name 第二次就放name+1 第三次就是 name+2
name就成了 name,name1,name2.......
[/Quote]

怎么加?
wozaiqingniao 2010-04-08
  • 打赏
  • 举报
回复
可以。你把name放进去的时候,比如说:第一次你放进去name 第二次就放name+1 第三次就是 name+2
name就成了 name,name1,name2.......
qlzf11140820 2010-04-08
  • 打赏
  • 举报
回复
肯定不能

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧