mercredi 13 avril 2016

"Object does not match target type" exception when reflecting over object properties

I'm updating some code so that it reflects into the models it receives and figures out what properties to write out fields in a PDF file rather than me subclassing and manually mapping the fields for each and every PDF. The problem is that I can't get the property's value because of an Object does not match target type exception. Looking through similar questions on SO it seems like a simple fix, but just doesn't want to work. Here's my code that I'm testing through LINQPad:

var pdf = typeof(PdfModel2);

pdf.GetProperties().Where(
    p =>
        p.GetCustomAttribute<PdfTextFieldAttribute>() != null).Select(
    p =>
        p.GetValue(pdf)).Dump();

Can someone point me where I'm going wrong here? Is it because the PdfModel2 class I'm reflecting over has properties its inheriting from a base class?





Aucun commentaire:

Enregistrer un commentaire