PropertyInfo SetValue doesn't work for custom objects.
-
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
-
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
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
-
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
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.:(