mercredi 22 août 2018

Trying to get the generic type from PropertyInfo [duplicate]

This question already has an answer here:

And let me say at the outset, that I know about the 'PropertyType' property.

So I've been working with a generic converter.

Usage is pretty simple; it looks like this:

int1 = TConverter.ChangeType<int>(strt1);

So that string becomes and int (if it can). And the above works fine. The problem, however, is that part between the angular brackets where the type is listed. I want to get the type of a PropertyInfo in there, but I can't see to.

The code, which I have simplified down to the main issue, looks like this:

foreach (PropertyInfo pi in props)
        {
            Type tp = pi.PropertyType;

            var converted = TConverter.ChangeType<tp>("Test");
        }

I guess I must be making a mistake because it has a problem with the contents of the angular brackets. I get this message: "Error 2 The type or namespace name 'tp' could not be found (are you missing a using directive or an assembly reference?)"

Can someone offer a solution?





Aucun commentaire:

Enregistrer un commentaire