日期:2014-05-17  浏览次数:21108 次

Winform如何获取控件的属性名和值.(所有的).
我觉得大家应该有过这样的想法.
因为很多时候,我看别人的代码的时候,他们设置的属性,有时在 CodeBehind中,有时在Properties窗口中,但是有时想比较自己设置的属性和他们的有哪些不同.一个一个地去找,简直不是程序员应该做的.是吧.这个时候该怎么办?

------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Test
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public DateTime CreateTime { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test t = new Test() { ID = 1, Name = "abc", CreateTime = DateTime.Now };
            var query = t.GetType().GetProperties().Select(x => string.Format("{0}: {1}.", x.Name, x.GetValue(t, null).ToString()));
            foreach (string prop in query)
            {
                Console.WriteLine(prop);
            }
        }
    }
}


ID: 1.
Name: abc.
CreateTime: 2013-9-23 1:07:50.
Press any key to continue . . .
------解决方案--------------------
可以通过反射获取相应的方法或者属性
曹版已经说出来了
t.GetType().GetProperties()
------解决方案--------------------
        public void GetParams()
        {
            inItem.Clear();
            this.GetElementInContainer((Control)HttpContext.Current.Handler, inItem);
        }

        public void GetParams(Control container)
        {
            inItem.Clear();
            this.GetElementInContainer(container, inItem);
        }

        private ItemCollection GetElementInCo