none
匿名变量的属性名能不能用变量表示 RRS feed

  • 问题

  • 匿名变量的属性名能不能用变量表示呢,或者说能不用把字典转化成匿名类呢

    比如string a="kkk";

    var s=new { "kkk"="ttt"};

    在上面的代码中new { "kkk"="ttt"}这个KKK能不能用a来代替呢

    或者说Dictionary<string, object>把这个的一个变量转成匿名类的对象呢

    2013年2月28日 6:07

答案

  • 就像你已经定义了一个类的属性,属性名当然是固定的,一旦定义恐怕无法再变更了。

    除非你使用4.0(net framework)中的动态类(dynamic)属性。代码大致如下:

    namespace CSharp
    {
        class A : DynamicObject
        {
            private Dictionary<stringobject> KeyValues = new Dictionary<stringobject>();
     
            //动态获取属性
            public override bool TryGetMember(GetMemberBinder binderout object result)
            {
                if (KeyValues.ContainsKey(binder.Name))
                {
                    result = KeyValues[binder.Name];
                    return true;
                }
                result = null;
                return false;
            }
     
            //动态增加设置属性
            public override bool TrySetMember(SetMemberBinder binderobject value)
            {
                if (KeyValues.ContainsKey(binder.Name))
                {
                    KeyValues[binder.Name= value;
                }
                else
                {
                    KeyValues.Add(binder.Namevalue);
                }
                return true;
            }
     
            //动态改变已有属性
            public void ChangeProperty(string oldnamestring newname)
            {
                if (KeyValues.ContainsKey(oldname))
                {
                    object obj = KeyValues[oldname];
                    KeyValues.Remove(oldname);
                    KeyValues.Add(newnameobj);
                }
            }
        }
     
        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

    2013年3月1日 2:12
    版主

全部回复

  • 就像你已经定义了一个类的属性,属性名当然是固定的,一旦定义恐怕无法再变更了。

    除非你使用4.0(net framework)中的动态类(dynamic)属性。代码大致如下:

    namespace CSharp
    {
        class A : DynamicObject
        {
            private Dictionary<stringobject> KeyValues = new Dictionary<stringobject>();
     
            //动态获取属性
            public override bool TryGetMember(GetMemberBinder binderout object result)
            {
                if (KeyValues.ContainsKey(binder.Name))
                {
                    result = KeyValues[binder.Name];
                    return true;
                }
                result = null;
                return false;
            }
     
            //动态增加设置属性
            public override bool TrySetMember(SetMemberBinder binderobject value)
            {
                if (KeyValues.ContainsKey(binder.Name))
                {
                    KeyValues[binder.Name= value;
                }
                else
                {
                    KeyValues.Add(binder.Namevalue);
                }
                return true;
            }
     
            //动态改变已有属性
            public void ChangeProperty(string oldnamestring newname)
            {
                if (KeyValues.ContainsKey(oldname))
                {
                    object obj = KeyValues[oldname];
                    KeyValues.Remove(oldname);
                    KeyValues.Add(newnameobj);
                }
            }
        }
     
        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

    2013年3月1日 2:12
    版主
  • 我提供一个思路:

    先把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);

    2013年3月1日 3:44