Winform如何获取控件的属性名和值.(所有的).

wonkju 2013-09-23 12:57:46
我觉得大家应该有过这样的想法.
因为很多时候,我看别人的代码的时候,他们设置的属性,有时在 CodeBehind中,有时在Properties窗口中,但是有时想比较自己设置的属性和他们的有哪些不同.一个一个地去找,简直不是程序员应该做的.是吧.这个时候该怎么办?
...全文
662 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunshine327 2013-09-27
  • 打赏
  • 举报
回复
我想知道怎么获取combobox的值啊
youzelin 2013-09-27
  • 打赏
  • 举报
回复
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 GetElementInContainer(Control container, ItemCollection sc)
        {
            foreach (Control control in container.Controls)
            {
jiaoshiyao 2013-09-27
  • 打赏
  • 举报
回复
可以通过反射获取相应的方法或者属性 曹版已经说出来了 t.GetType().GetProperties()
threenewbee 2013-09-23
  • 打赏
  • 举报
回复
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 . . .

110,546

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧