如何将一个string的字符串转成一个对象变量

limon758 2008-09-26 06:24:04
就比如这样

string d1="aa";
string d2="bb";
string d3="cc";
string d4="dd";
string temp = "";

for (int i=1;i<5;i++)
{
temp += ("d" + i.ToString());
MessageBox.Show(temp);
}


想要的结果是 aabbccdd

能否实现`?
...全文
324 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
huawenlong001 2008-09-29
  • 打赏
  • 举报
回复
反射
xiaoping04408 2008-09-27
  • 打赏
  • 举报
回复
是的,不错.!
qinhl99 2008-09-27
  • 打赏
  • 举报
回复
string d1="aa";
string d2="bb";
string d3="cc";
string d4="dd";
string temp = "";

for (int i=1;i<5;i++)
{
temp += ("d" + i.ToString());
MessageBox.Show(temp);
}
当然不是可以的了!temp岂不是成了d1d2d3d4了,您好逗!
shanying_0 2008-09-27
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 justindreams 的回复:]
C# code

public string d1 = "aa";
public string d2 = "bb";
public string d3 = "cc";
public string d4 = "dd";

private void button1_Click(object sender, EventArgs e)
{
string temp = "";
Type type = this.GetType();
for (int i = 1; i < 5; i++)
{
FieldInfo fi = type.GetField("…
[/Quote]

这个不是饶了个大弯子的么。实现这样的。很可能是程序上的设计问题。建立修改一下。这样的话也很维护呀
diffmaker 2008-09-27
  • 打赏
  • 举报
回复
还没有用过反射,标记一下以后学习
wjq 2008-09-26
  • 打赏
  • 举报
回复
反射.....
同9楼
justindreams 2008-09-26
  • 打赏
  • 举报
回复


public string d1 = "aa";
public string d2 = "bb";
public string d3 = "cc";
public string d4 = "dd";

private void button1_Click(object sender, EventArgs e)
{
string temp = "";
Type type = this.GetType();
for (int i = 1; i < 5; i++)
{
FieldInfo fi = type.GetField("d" + i.ToString());
temp += fi.GetValue(this).ToString();
}
MessageBox.Show(temp);
}
wartim 2008-09-26
  • 打赏
  • 举报
回复
[code=C#]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Reflection;

namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public String d1
{
get
{
return "aa";
}
}

public String d2
{
get
{
return "bb";
}
}

public String d3
{
get
{
return "cc";
}
}

public String d4
{
get
{
return "dd";
}
}

public Form1()
{
InitializeComponent();

String Temp = "";
Type ThisType = this.GetType();
for (int i = 1; i < 5; i++)
{

PropertyInfo PI = ThisType.GetProperty("d" + i.ToString());
Temp += PI.GetValue(this, null).ToString();
}
MessageBox.Show(Temp);
}
}
}
code]
slin60 2008-09-26
  • 打赏
  • 举报
回复
一般来说你如果需要这种功能,往往是由于你设计不当造成的
patrickpan 2008-09-26
  • 打赏
  • 举报
回复
可以实现的。
用反射试试
gxtiou 2008-09-26
  • 打赏
  • 举报
回复
试试序列化生成对象
byKid 2008-09-26
  • 打赏
  • 举报
回复
我知道你想获得一个PHP编程那种变量传递的效果。

但是C#没有这个功能。
limon758 2008-09-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hackztx 的回复:]
string d1 = "aa";
string d2 = "bb";
string d3 = "cc";
string d4 = "dd";
string strtmp= string.Concat(d1,d2,d3,d4);
[/Quote]

这个不对,你没有把对象变成字符串

要把d1变成 "d1"这个形式,再转成对象然后输出里边的值
wdgphc 2008-09-26
  • 打赏
  • 举报
回复
不能的.
hackztx 2008-09-26
  • 打赏
  • 举报
回复
string d1 = "aa";
string d2 = "bb";
string d3 = "cc";
string d4 = "dd";
string strtmp= string.Concat(d1,d2,d3,d4);

110,567

社区成员

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

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

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