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
  1. Home
  2. General Programming
  3. C#
  4. PropertyInfo SetValue doesn't work for custom objects.

PropertyInfo SetValue doesn't work for custom objects.

Scheduled Pinned Locked Moved C#
helpcareer
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.
  • M Offline
    M Offline
    Matt Casto
    wrote on last edited by
    #1

    I'll start my problem description off with some sample code which will probably do the best job at illustrating the problem. using System; namespace TestCustomTypePropertySetValue {   public class CustomThing   {     public CustomThing(string input)     {       m_input = input;     }     public string Input     {       get { return m_input; }     }     private string m_input;     public override string ToString()     {       return this.Input;     }     public static implicit operator CustomThing(string input)     {       return new CustomThing(input);     }     public static implicit operator string(CustomThing thing)     {       return thing.ToString();     }   }   public class ClassWithThing   {     public CustomThing Thing     {       get { return m_thing; }       set { m_thing = value; }     }     private CustomThing m_thing = new CustomThing("default");   }   class Class1   {     [STAThread]     static void Main(string[] args)     {       // set a custom thing to a string - this works       CustomThing thing = "test custom thing input";              // set a string to a custom thing - this works       string input = thing;       // a property of type CustomThing       ClassWithThing cwt = new ClassWithThing();       System.Reflection.PropertyInfo propInfo =         cwt.GetType().GetProperty("Thing");       // set the property to a string value - this fails       p

    X 1 Reply Last reply
    0
    • M Matt Casto

      I'll start my problem description off with some sample code which will probably do the best job at illustrating the problem. using System; namespace TestCustomTypePropertySetValue {   public class CustomThing   {     public CustomThing(string input)     {       m_input = input;     }     public string Input     {       get { return m_input; }     }     private string m_input;     public override string ToString()     {       return this.Input;     }     public static implicit operator CustomThing(string input)     {       return new CustomThing(input);     }     public static implicit operator string(CustomThing thing)     {       return thing.ToString();     }   }   public class ClassWithThing   {     public CustomThing Thing     {       get { return m_thing; }       set { m_thing = value; }     }     private CustomThing m_thing = new CustomThing("default");   }   class Class1   {     [STAThread]     static void Main(string[] args)     {       // set a custom thing to a string - this works       CustomThing thing = "test custom thing input";              // set a string to a custom thing - this works       string input = thing;       // a property of type CustomThing       ClassWithThing cwt = new ClassWithThing();       System.Reflection.PropertyInfo propInfo =         cwt.GetType().GetProperty("Thing");       // set the property to a string value - this fails       p

      X Offline
      X Offline
      Xint0
      wrote on last edited by
      #2

      Matt Casto wrote:

      If you run the sample code, you might notice that the SetValue doesn't even call the implicit conversion from string to CustomThing.

      That's because SetValue uses Object type for the value. Try adding an implicit conversion from Object to CustomThing.

      - Xint0

      M 1 Reply Last reply
      0
      • X Xint0

        Matt Casto wrote:

        If you run the sample code, you might notice that the SetValue doesn't even call the implicit conversion from string to CustomThing.

        That's because SetValue uses Object type for the value. Try adding an implicit conversion from Object to CustomThing.

        - Xint0

        M Offline
        M Offline
        Matt Casto
        wrote on last edited by
        #3

        Xint0 wrote:

        Try adding an implicit conversion from Object to CustomThing.

        I already thought of that, but it doesn't even compile because you can't have a user-defined conversion to or from the base class, so object is definitely out for that.:(

        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