CorrieJanse
CorrieJanse

Reputation: 2598

Object does not match target type reflection error

I am getting the error Object does not match target type. I understand why I should be getting the error. But I am assigning a double to a double. I am not sure why I am getting the error in the code below.

The code I am having issue with:

...

var specTran = new SpecTran(gapRecord);
foreach (var result in yields._predictions)
{
    var type = specTran.GetType(); // --> {Name = "SpecTran" FullName = "GAP.Models.Database.SpecTran"}
    var prop = type.GetProperty(result.Key); // --> {Double DexaCWT}

    if (prop == null)
        continue;

    prop.SetValue(
        (double)result.Value, // --> {[DexaCWT, 0]}
        specTran);
}

PredictionResult class:

PredictionResult {
    ...
    public Dictionary<string, double> _predictions { get; set; }
}

Upvotes: 1

Views: 655

Answers (1)

David L
David L

Reputation: 33815

You're getting the error because your arguments to SetValue are backwards, per the docs.

It should be:

prop.SetValue(
    specTran,    
    (double)result.Value); // --> {[DexaCWT, 0]}

Upvotes: 1

Related Questions