title: Use reflection to set a property value to Nothing (Null)
body: <p>Edit: Based on the answer from LoveMeSomeCode, I believe this issue only appears in VB.Net.</p>
  
  <p>I'm trying to revert a class to a previous state by saving the old values of changed properties in a dictionary and setting them via reflection when I need to revert. I'm having a problem where if the old value is Nothing (null) I get a null reference exception when trying to set the property. Here's what I've tried.</p>
  
  <p>Assume a for each loop like this:</p>
  
  <pre><code>For Each pair As KeyValuePair(Of String, Object) In myOldValues
  ...
  Next
  </code></pre>
  
  <p>Method 1:</p>
  
  <pre><code>CallByName(Me, pair.Key, CallType.Set, pair.Value)
  </code></pre>
  
  <p>Method 2:</p>
  
  <pre><code>Me.GetType().InvokeMember(pair.Key, Reflection.BindingFlags.SetProperty, Nothing, Me, pair.Value)
  </code></pre>
  
  <p>Method 3:</p>
  
  <pre><code>Dim propInfo As System.Reflection.PropertyInfo = Me.GetType.GetProperty(pair.Key)
  propInfo.SetValue(Me, Convert.ChangeType(pair.Value, propInfo.PropertyType), Nothing)
  </code></pre>
  
  <p>For each of these methods I get a null reference exception when pair.Value is null. The setter is capable of holding a null value (frequently the property is a string). What am I doing wrong or how can I work around it?</p>
  
  <p><strong>Edit:</strong> Each method fails if I pass it null directly as well.</p>
  
  <p><strong>Edit:</strong> Here are the stack traces if they help anyone:</p>
  
  <p><strong>Method 1</strong>
  System.NullReferenceException: Object reference not set to an instance of an object.
     at Microsoft.VisualBasic.CompilerServices.Symbols.Container.InvokeMethod(Method TargetProcedure, Object[] Arguments, Boolean[] CopyBack, BindingFlags Flags)
     at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateSet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean OptimisticSet, Boolean RValueBase, CallType CallType)
     at Microsoft.VisualBasic.CompilerServices.Versioned.CallByName(Object Instance, String MethodName, CallType UseCallType, Object[] Arguments)
     at <em>myProject</em>.Presenter.CustomerDetailPresenter.RevertCustomer() in <em>myfile</em>:line 378</p>
  
  <p><strong>Method 2</strong>
  System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
     at <em>myProject</em>.Presenter.CustomerDetailPresenter.set_City(String value)
     --- End of inner exception stack trace ---
     at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct&amp; sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
     at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
     at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
     at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
     at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
     at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)
     at <em>myProject</em>.Presenter.CustomerDetailPresenter.RevertCustomer()</p>
  
  <p><strong>Method 3</strong>
  System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
     at <em>myProject</em>.Presenter.CustomerDetailPresenter.set_City(String value)
     --- End of inner exception stack trace ---
     at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct&amp; sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
     at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
     at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)</p>
  
  <p>at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
     at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
     at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)
     at <em>myProject</em>.Presenter.CustomerDetailPresenter.RevertCustomer()</p>