标签:
根据反射为类中的属性赋值:
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); } } } }
结果:
标签:
原文地址:http://www.cnblogs.com/jing89/p/4268383.html