In c# convert anonymous type into key/value array?

前端 未结 9 1447
囚心锁ツ
囚心锁ツ 2020-12-04 23:28

I have the following anonymous type:

new {data1 = \"test1\", data2 = \"sam\", data3 = \"bob\"}

I need a method that will take this in, and

9条回答
  •  攒了一身酷
    2020-12-04 23:41

    Here is how they do it in RouteValueDictionary:

      private void AddValues(object values)
        {
            if (values != null)
            {
                foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(values))
                {
                    object obj2 = descriptor.GetValue(values);
                    this.Add(descriptor.Name, obj2);
                }
            }
        }
    

    Full Source is here: http://pastebin.com/c1gQpBMG

提交回复
热议问题