vendredi 13 mai 2016

Setting a property using reflection which may not be present

When trying to set a property added in .Net 4.5 on a system which only has .Net 4.0 you get MissingMemberException (http://ift.tt/1sjGf9n). However you can only catch this when using reflection, otherwise it is an uncatchable JIT exception. (Why is it not possible to catch MissingMethodException?)

So I changed my code from:

client.DeliveryFormat = SmtpDeliveryFormat.International;

to

client.GetType().GetProperty("DeliveryFormat").SetValue(client, SmtpDeliveryFormat.International);

However now I get a TypeLoadException thrown instead... how can I safely write my code in this case (assuming this property is not essential to our application)?





Aucun commentaire:

Enregistrer un commentaire