jeudi 2 juin 2016

How to set the value of a property to null using reflection

I want to set the value of LastName to null using reflection.

public void SetPropertyValueToNull(Type t)
{
    PropertyInfo prop = t.GetProperty("LastName");
    prop.SetValue(t, null, null);
}

I am passing typeof(User) as the parameter from another class where User is a model class which has LastName as one of the properties. It gives me Object does not match target type error.

How can I resolve this issue?





Aucun commentaire:

Enregistrer un commentaire