I am trying to fill out a pdf form using reflection as below:
// Get the form fields for this PDF and fill them in!
var formFieldMap = GetFormFieldNames(pdfTemplate);
foreach (var fields in formFieldMap)
{
var fieldName = UppercaseFirst(fields.Key.Replace("pdf_", ""));
var proPertyValue = fosques.GetType().GetProperty(fieldName).GetValue(ques, null);
formFieldMap[fields.Key] = proPertyValue.ToString();
}
This works fine for first property, but on second iteration it say Collection was modified; enumeration operation may not execute.
Should I create another collection of fields, or can I avoid this?
You are referencing the list you are enumerating through inside the enumeration.
You don’t need to get
formFieldMapy[fields.Key]because you already have access to it, so you could simply do:The above does the same thing, it just doesn’t directly modify what you are enumerating.