类A 里面有属性p1 p2 p3 p4 p5 .....(若干,也可能没有)
数据库T 
表里列C
c1
c2
c4
c5
c6
c7
....
如何才能把值赋给属性?
例如 p1=c1 p2=c2 (p3不赋值) p4=c4 p5=c5 (如果没有p6就不赋值) ....麻烦写一下关键代码,谢谢.

解决方案 »

  1.   

    search in http://www.baidu.com
      

  2.   

    class A
            {
                public string p1 { get; set; }
                public string p2 { get; set; }
                public string p3 { get; set; }
                public string p4 { get; set; }
                public string p5 { get; set; }
            }
     A a = new A();
                var ary = @"c1
    c2
    c4
    c5
    c6
    c7".Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var t in ary)
                {
                    var Property = typeof(A).GetProperty("p" + Regex.Replace(t, @"\D", ""));
                    if (Property != null)
                        Property.SetValue(a, t, null);
                }