使用反射 public class Student { private string name; public string Name { get { return this.name; } set { this.name = value; } } }参考下吧 string a = "Name"; Student s = new Student(); Type t = s.GetType(); PropertyInfo[] p = t.GetProperties(); foreach (PropertyInfo pi in p) { if (pi.Name == a) { pi.SetValue(s, "Neal", null); } } Console.WriteLine(s.Name);
A p=new A(); var info = p.GetType().GetProperty(""); if (info != null) { info.SetValue(p, "", null); }
参考
http://blog.csdn.net/tianyu00/archive/2007/05/21/1618723.aspx
string a="+NAME+"
那个NAME可以是动态的,比如是XX TABLE里的
你可以绑定了TABLE XX=new table()
XX.NAME放进去
{
private string name;
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
}参考下吧 string a = "Name";
Student s = new Student();
Type t = s.GetType();
PropertyInfo[] p = t.GetProperties();
foreach (PropertyInfo pi in p)
{
if (pi.Name == a)
{
pi.SetValue(s, "Neal", null);
}
}
Console.WriteLine(s.Name);
var info = p.GetType().GetProperty("");
if (info != null)
{
info.SetValue(p, "", null);
}
UserInfo ui=new UserInfo();
ui.name=值;
上述代码应该稍加改动:
Student student = new Student(a); //此处不进行初始化如何进行比对?
让您见笑,我收回才刚的代码,是我理解错了.
pi.Name获取的是公共属性名字符串!支持正确的说法!
get
{
return name;
}
set
{
name=value;
}
get
{
return name;
}
set
{
name=value;
}