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

kalno12

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?

Yacoub Massad

It gives me Object does not match target type error.

The first parameter of the SetValue method is the object instance on which you want to set the property value.

You are passing t itself as the first parameter which is of type Type. You should instead pass an object that has a type equal to t, e.g. User in your specific case.

You should change your method to accept an object instance instead of a Type and you can use it to get its type, get the property and then set its value.

So your method should look something like this:

public void SetPropertyValueToNull(object instance)
{
    PropertyInfo prop = instance.GetType().GetProperty("LastName");
    prop.SetValue(instance, null, null); //We need this overload for .NET < 4.5
}

This method will work for any object that has a writable property named LastName.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Get property value from string using reflection

From Dev

How to set Project property value using Groovy?

From Dev

how to read value of style["display"] property of webcontrol using reflection

From Dev

Cannot set property value of null

From Dev

How to set null to a GUID property

From Dev

Property reflection - How to get value?

From Dev

Uncaught TypeError: Cannot set property 'value' of null

From Dev

Why is it possible to set a value in a slice using reflection?

From Dev

using reflection to set member's value

From Dev

Set property values of an Objective-C class using reflection

From Dev

Is there any other approach to set a value to a Property (of an Instance) without using Reflection?

From Dev

Cannot set property 'value' of null

From Dev

How to get value of property from object of class at runtime using reflection

From Dev

Using Reflection.Emit to set a property value

From Dev

Set auto-property's value via reflection

From Dev

How to get a property name and its value using Swift 2.0, and reflection?

From Dev

Spring SpEL - set a property only if the value is not null

From Dev

How can I check if a property has been set using Swift reflection?

From Dev

How to use reflection to get/set the value of a property?

From Dev

Set Property name and Null Value Handling on JsonProperty

From Dev

How to set Array using reflection

From Dev

How to create a property on a dynamic object using reflection

From Dev

Set auto-property's value via reflection

From Dev

How can I Get the property by name from type and Set Propert value using reflection in C#

From Dev

How to invoke a method with null parameter value using reflection in groovy?

From Dev

How to set nested property value using FastMember

From Dev

Setting Property Value using Reflection

From Dev

Set property of property of property using reflection

From Dev

Set readonly property value through reflection

Related Related

  1. 1

    Get property value from string using reflection

  2. 2

    How to set Project property value using Groovy?

  3. 3

    how to read value of style["display"] property of webcontrol using reflection

  4. 4

    Cannot set property value of null

  5. 5

    How to set null to a GUID property

  6. 6

    Property reflection - How to get value?

  7. 7

    Uncaught TypeError: Cannot set property 'value' of null

  8. 8

    Why is it possible to set a value in a slice using reflection?

  9. 9

    using reflection to set member's value

  10. 10

    Set property values of an Objective-C class using reflection

  11. 11

    Is there any other approach to set a value to a Property (of an Instance) without using Reflection?

  12. 12

    Cannot set property 'value' of null

  13. 13

    How to get value of property from object of class at runtime using reflection

  14. 14

    Using Reflection.Emit to set a property value

  15. 15

    Set auto-property's value via reflection

  16. 16

    How to get a property name and its value using Swift 2.0, and reflection?

  17. 17

    Spring SpEL - set a property only if the value is not null

  18. 18

    How can I check if a property has been set using Swift reflection?

  19. 19

    How to use reflection to get/set the value of a property?

  20. 20

    Set Property name and Null Value Handling on JsonProperty

  21. 21

    How to set Array using reflection

  22. 22

    How to create a property on a dynamic object using reflection

  23. 23

    Set auto-property's value via reflection

  24. 24

    How can I Get the property by name from type and Set Propert value using reflection in C#

  25. 25

    How to invoke a method with null parameter value using reflection in groovy?

  26. 26

    How to set nested property value using FastMember

  27. 27

    Setting Property Value using Reflection

  28. 28

    Set property of property of property using reflection

  29. 29

    Set readonly property value through reflection

HotTag

Archive