Im using the Microsoft Community MVVM package within my MAUI app. It's working well, but I've hit an issue.
Part of my UI modifies a structure which sends a message to the view model. This works fine. However, when I try to modify the structure, I'm getting an exception (Object does not match target type.
)
The code leading up to the exception looks like this
messenger.Register<ObjectMessage>(this, (o, t) =>
{
switch (t.Sender)
{
case "Changes":
try
{
var prop = Content.Structure.GetType().GetProperty((string)t.Message);
prop.SetValue(t.Value, Convert.ChangeType(t.Value, prop.PropertyType));
}
catch(Exception ex)
{
#if DEBUG
Console.WriteLine($"Exception - ex.Message = {ex.Message}");
#endif
}
HasChanges = Content.Structure != DupeContent.Structure;
break;
}
});
I've tried a number of different ways to assign the value to the property, but always get the exception.
t.Value
and t.Message
are of type object
The first argument to
SetValue
should be the actual object you're trying to modify. In this case, it appears to beContent.Structure
(since that's what you calledGetType()
on). Try using that object as the first argument: