This is a command system that runs using Command Attributes. An example of how this works is listed below.
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.
When setting the value of a property located in the CommandContext Class (Message in this case) I am receiving the following error.
Object does not match target type
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.
Error is occurring here:
messageProp.SetValue(baseType, Convert.ChangeType(rawMessage, messageProp.PropertyType), null);
BASE CLASS
public class CommandContext
{
public string Message { get; internal set; }
public CommandContext() { }
}
DERIVED CLASS
public class Maintenance : CommandContext
{
[Command("message")]
public void SendMessage()
{
Console.WriteLine(Message);
}
}
METHOD
string rawMessage = messageData.SelectToken("msg")?.ToString();
if (rawMessage[0] == _commandPrefix)
{
var method = Assembly.GetEntryAssembly()
.GetTypes()
.SelectMany(t => t.GetMethods())
.FirstOrDefault(m =>
m.GetCustomAttribute<CommandAttribute>()?.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);
}
else
new Channel(messageData.SelectToken("rid")?.ToString()).SendMessage("Command does not exist");
}
Aucun commentaire:
Enregistrer un commentaire