title: C# Reflection - PropertyInfo.SetValue [Object does not match target type]
body: <p>This is a command system that runs using Command Attributes. An example of how this works is listed below.</p>
  
  <p>If you were to type /message in the chat, this will run the method in the entry assembly that contains a CommandAttribute with the Text value of "message". All Classes that use the CommandAttribute inherit from the CommandContext Class. Using reflection, I am trying to set the value of the CommandContext properties so that they can be used in the Derived Class which would contain the Command Method that was invoked.</p>
  
  <p>When setting the value of a property located in the CommandContext Class (Message in this case) I am receiving the following error.</p>
  
  <blockquote>
    <p>Object does not match target type</p>
  </blockquote>
  
  <p>I have tried the solutions from other questions but I am still receiving the error.
  I have posted the Derived class, Base Class, and the Method below. Please let me know if there is any other information needed to help me out. Thank you all for your help.</p>
  
  <p><strong>Error is occurring here:</strong> </p>
  
  <blockquote>
    <p><code>messageProp.SetValue(baseType, Convert.ChangeType(rawMessage, messageProp.PropertyType), null);</code></p>
  </blockquote>
  
  <p>COMMAND ATTRIBUTE</p>
  
  <pre><code>namespace RocketNET.Attributes
  {
      public class CommandAttribute : Attribute
      {
          public string Text { get; private set; }
          public CommandAttribute(string text)
          {
              Text = text;
          }
      }
  }
  </code></pre>
  
  <p>BASE CLASS</p>
  
  <pre><code>namespace RocketNET
  {
      public class CommandContext
      {
          public string Message { get; internal set; }
  
          public CommandContext() { }
      }
  }
  </code></pre>
  
  <p>DERIVED CLASS</p>
  
  <pre><code>namespace ACGRocketBot.Commands
  {
      public class Maintenance : CommandContext
      {
          [Command("message")]
          public void SendMessage()
          {
              Console.WriteLine(Message);
          }
      }
  }
  </code></pre>
  
  <p>METHOD</p>
  
  <pre><code>namespace RocketNET
  {
      public class RocketClient
      {
          private void MessageReceived(object sender, MessageEventArgs e)
          {
              string rawMessage = "/message";
  
              if (rawMessage[0] == _commandPrefix)
              {
                  var method = Assembly.GetEntryAssembly()
                      .GetTypes()
                      .SelectMany(t =&gt; t.GetMethods())
                      .FirstOrDefault(m =&gt;
                          m.GetCustomAttribute&lt;CommandAttribute&gt;()?.Text == rawMessage.Substring(1).ToLower());
  
  
                  if (method != null)
                  {
                      method.Invoke(Activator.CreateInstance(method.DeclaringType), null);
  
                      var baseType = method.DeclaringType.BaseType;
                      var messageProp = baseType.GetProperty("Message");
  
                      messageProp.SetValue(baseType, Convert.ChangeType(rawMessage, messageProp.PropertyType), null);
                  }
              }
          }
      }
  }
  </code></pre>