1

I'm trying to check if webcontrol supports a particular property (although it can be any object) - if it does I want to check if a value exists - if a value exists, I want to do nothing, otherwise I'll update it with a given value.

Here's my code:

    public static void SetProperty(this object @this, string name, object value)
    {
        var p = @this.GetType().GetProperty(name);
        if (p != null)
        {
            var v = p.GetValue(@this, null);
            if(v == null) p.SetValue(@this, value, null);
        }
    }

The problem is that v is never NULL, but it doesn't seem to contain a value either, which is baffling me. If I remove the if(v == null) it works, but with it there is no update.

I'm testing it out on a hyperlink Text and NavigateUrl properties.

Any help appreciated.

John Ohara
  • 2,821
  • 3
  • 28
  • 54
  • Maybe this question may help you: http://stackoverflow.com/questions/65351/null-or-default-comparison-of-generic-argument-in-c-sharp – Manuel Zelenka Sep 21 '15 at 15:54
  • I don't understand what the post has to do with my question. – John Ohara Sep 21 '15 at 16:08
  • You are trying to look for a default value (null) but not all types have null as their default value. E.g. value types like int, bool, ... Therefore I have provided you with a question where in the answers you find a way to look for any default value – Manuel Zelenka Sep 22 '15 at 10:48

0 Answers0