Activator.CreateInstance 反射实例化对象

public class CommonReq

{

   private String TransNo { get; set;}

  public String SubmitData { get; set; }

      private String Sign { get; set; }

  public String Encoding { get; set; }

public CommonReq()

{

   this.TransNo = Guid.NewGuid().ToString(); }

}

 

class Program

{

  static void Main(string[] args)

   {

 

    //2种方式都会调用 空参数的构造函数 如果不提供空参数构造函数或不默认提供,则无法运行CreateInstance

    CommonReq req = new CommonReq { SubmitData="A=1", Encoding="utf-8" };

       var req2 = Activator.CreateInstance(typeof(CommonReq)) as CommonReq;

  }

posted @ 2016-04-28 10:00  zslm___  阅读(1673)  评论(0编辑  收藏  举报