vendredi 24 août 2018

C# reflection object does not match the target type

I have read the other questions in regards to this error with reflection however none of the answers are fixing my issue. I have tried tons of different variations and each one is throwing the same error.

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), new object[] { rawMessage });

                        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