Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PropertyInfo GetValue() Object does not match target type

Tags:

c#

types

I want to read value of a T type

  public virtual ActionResult Edit(TEditDTO editedDTO)
    {
        if (!ModelState.IsValid) return View(editedDTO);
        var t = editedDTO.GetType();
        var prop = t.GetProperty("Id") ;
        var Id = prop.GetValue(t); // get exception
     }

but get

Object does not match target type

like image 958
Mohammadreza Avatar asked Jul 10 '14 09:07

Mohammadreza


Video Answer


2 Answers

You should pass the instance of TEditDTO to GetValue method not the type instance.

var Id = prop.GetValue(editedDTO);
like image 61
Selman Genç Avatar answered Sep 22 '22 13:09

Selman Genç


Try this,

var Id = prop.GetValue(editedDTO, null);
like image 41
Yuliam Chandra Avatar answered Sep 19 '22 13:09

Yuliam Chandra