Object does not match target type
-
the sample class
public class Foo{
public string Name {get;set;}
public bool IsLocked {get;set;}
public double LockID {get;set;}
}and i have this method
public dictionary GetSettings()
{
//read the setting files using the FIle.ReadAllLines(pathtosetting)return somethig;
}then when i access it like this
var sample = new Foo();
var prop = sample.GetType();
foreach(var item in GetSettings())
{
//the item.key is the property name of Foo
prop.GetProperty(item.key, BindingFlags.public | BindingFlags.Instance).SetValue(prop, item.Value, null)
}then i got an exception "Object does not match target type" I cant find any solution on the web I will appreciate for any advice will come thank you
-
the sample class
public class Foo{
public string Name {get;set;}
public bool IsLocked {get;set;}
public double LockID {get;set;}
}and i have this method
public dictionary GetSettings()
{
//read the setting files using the FIle.ReadAllLines(pathtosetting)return somethig;
}then when i access it like this
var sample = new Foo();
var prop = sample.GetType();
foreach(var item in GetSettings())
{
//the item.key is the property name of Foo
prop.GetProperty(item.key, BindingFlags.public | BindingFlags.Instance).SetValue(prop, item.Value, null)
}then i got an exception "Object does not match target type" I cant find any solution on the web I will appreciate for any advice will come thank you
Means that the type which the property expects is different from the type you are putting in there. You've got a lot going on in a single line of reflection, I'd recommend to break it up in atomic statements; first get the property and put that in a PropInfo. Check if you found it, and then assign a value. Makes debugging a lot easier. By the looks, I'd say the the boolean and the double are being fed strings.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
-
Means that the type which the property expects is different from the type you are putting in there. You've got a lot going on in a single line of reflection, I'd recommend to break it up in atomic statements; first get the property and put that in a PropInfo. Check if you found it, and then assign a value. Makes debugging a lot easier. By the looks, I'd say the the boolean and the double are being fed strings.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
I just discard the reflection, i found no benefit using the reflection for that method other than writing a less code.