Here is a Code-Snippet from a generic CSV -> Object Importer/Mapper I wrote. At some point I ran into an issue with Nullable types / values which I had to set (and convert) based on strings from the CSV file.
public virtual void HandlePropertyAssignment(PropertyInfo pi, string propertyValue, T instance) { try { if ( pi.PropertyType.GetGenericArguments() != null && pi.PropertyType.GetGenericArguments().Length > 0 && ) { Type genericType = pi.PropertyType.GetGenericArguments()[0]; pi.SetValue(instance, Convert.ChangeType(propertyValue, genericType), null); } else { pi.SetValue(instance, Convert.ChangeType(propertyValue, pi.PropertyType), null); } } catch (System.Exception ex) { propertyValue,pi.Name,pi.PropertyType),ex); } }
Comments
Thx man.
Thx man.
Thanks man! Very useful tip!
Thanks man!
Very useful tip!
Great post, thanks!
Great post, thanks!
Thankyou! Thankyou! Thankyou!
This was driving me crazy and I did not find documentation anywhere else.
Thanks for that snippet :)
Yesterday I was searching all the time for setting nullable types via reflection. Today, first hit was your blog and now the secret is raised :)
greets
paddy