简体   繁体   中英

How to get back the value using reflection in iTextSharp

I have a requirement in which user select the size of pdf that is generated dynamically.

To populate all sizes supported by iTextSharp I am enumerating all sizes in a dropdownlist as

      System.Reflection.FieldInfo[] fi = typeof(iTextSharp.text.PageSize).GetFields();
      DropDownList1.DataSource = fi;
      DropDownList1.DataBind();

Every thing is ok till here. Now when user select say letter size, how can i use this information for initializing the document which is initialized like

var document = new Document(PageSize.LETTER);

Currently i am trying to get it like this but its giving compile type error.

PageSize getpsize()
 {
      System.Reflection.FieldInfo[] fi = typeof(iTextSharp.text.PageSize).GetFields();
      int si = DropDownList1.SelectedIndex;
      PageSize p = fi[si];
      return p;
 }

Please help as this is my first serious experience with reflection.

System.Reflection.FieldInfo[] fi = typeof(iTextSharp.text.PageSize).GetFields();

FieldInfo[] is array of fields metadata, not the objects themselves.

So after you get the necessary metadata, you need to get actual field value like this:

FieldInfo field = fi[si];
PageSize size = (PageSize)field.GetValue(null);

GetValue(object) returns actual value of the field. As these fields are static, you pass null, as there is no specific object to query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM