积极答复者
匿名变量的属性名能不能用变量表示

问题
答案
-
就像你已经定义了一个类的属性,属性名当然是固定的,一旦定义恐怕无法再变更了。
除非你使用4.0(net framework)中的动态类(dynamic)属性。代码大致如下:
namespace CSharp { class A : DynamicObject { private Dictionary<string, object> KeyValues = new Dictionary<string, object>(); //动态获取属性 public override bool TryGetMember(GetMemberBinder binder, out object result) { if (KeyValues.ContainsKey(binder.Name)) { result = KeyValues[binder.Name]; return true; } result = null; return false; } //动态增加设置属性 public override bool TrySetMember(SetMemberBinder binder, object value) { if (KeyValues.ContainsKey(binder.Name)) { KeyValues[binder.Name] = value; } else { KeyValues.Add(binder.Name, value); } return true; } //动态改变已有属性 public void ChangeProperty(string oldname, string newname) { if (KeyValues.ContainsKey(oldname)) { object obj = KeyValues[oldname]; KeyValues.Remove(oldname); KeyValues.Add(newname, obj); } } } class Program { static void Main(string[] args) { dynamic a = new A(); a.Property1 = "A"; Console.WriteLine(a.Property1); //更改Property1到Property2 a.ChangeProperty("Property1", "Property2"); //打印 Console.WriteLine(a.Property2); } } }
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats- 已标记为答案 Lisa ZhuModerator 2013年3月11日 7:36
全部回复
-
就像你已经定义了一个类的属性,属性名当然是固定的,一旦定义恐怕无法再变更了。
除非你使用4.0(net framework)中的动态类(dynamic)属性。代码大致如下:
namespace CSharp { class A : DynamicObject { private Dictionary<string, object> KeyValues = new Dictionary<string, object>(); //动态获取属性 public override bool TryGetMember(GetMemberBinder binder, out object result) { if (KeyValues.ContainsKey(binder.Name)) { result = KeyValues[binder.Name]; return true; } result = null; return false; } //动态增加设置属性 public override bool TrySetMember(SetMemberBinder binder, object value) { if (KeyValues.ContainsKey(binder.Name)) { KeyValues[binder.Name] = value; } else { KeyValues.Add(binder.Name, value); } return true; } //动态改变已有属性 public void ChangeProperty(string oldname, string newname) { if (KeyValues.ContainsKey(oldname)) { object obj = KeyValues[oldname]; KeyValues.Remove(oldname); KeyValues.Add(newname, obj); } } } class Program { static void Main(string[] args) { dynamic a = new A(); a.Property1 = "A"; Console.WriteLine(a.Property1); //更改Property1到Property2 a.ChangeProperty("Property1", "Property2"); //打印 Console.WriteLine(a.Property2); } } }
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats- 已标记为答案 Lisa ZhuModerator 2013年3月11日 7:36
-
我提供一个思路:
先把Dictionary<string,object>序列化string,然后在把string反序列化匿名对象。
代码如下:
// 数据源 IDictionary<string, object> dic = new Dictionary<string, object>(); dic.Add("a", "aa"); dic.Add("b", "bb"); dic.Add("c", "cc"); // 序列化 JavaScriptSerializer serializer = new JavaScriptSerializer(); string serialStr = serializer.Serialize(dic); // 反序列化 object varObj = serializer.DeserializeObject(serialStr);