首页 > Windows开发 > 详细

c# 反射1

时间:2015-02-02 19:40:01      阅读:348      评论:0      收藏:0      [点我收藏+]

根据反射为类中的属性赋值:

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Reflection;


namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            TestA testa = new TestA();
            testa.SetValue();
            testa.Show();
            Console.Read();
        }
    }

    class TestA : TestBase
    {
        public string pa { get; set; }
        private string pb { get; set; }

        public void Show()
        {
            Console.WriteLine("PA:" + pa);
            Console.WriteLine("PB:" + pb);
        }

    }


    public class TestBase
    {
        public void SetValue()
        {
            Type type = this.GetType();
            PropertyInfo[] propertys = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo item in propertys)
            {
                item.SetValue(this, item.Name);
            }
        }
    }
}

结果:

技术分享

c# 反射1

原文:http://www.cnblogs.com/jing89/p/4268383.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!