Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Object does not match target type

Object does not match target type

Scheduled Pinned Locked Moved C#
regex
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gilbert Consellado
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • G Gilbert Consellado

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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)

      G 1 Reply Last reply
      0
      • L Lost User

        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)

        G Offline
        G Offline
        Gilbert Consellado
        wrote on last edited by
        #3

        I just discard the reflection, i found no benefit using the reflection for that method other than writing a less code.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups