I'm trying to get a float property with reflection and use its ToString(IFormatProvider) method to set another property, type of string. I get "Reflection.TargetException, Object does not match target type" error. I will put some code below to explain it:
public class myForm : Form
{
public float myFloat { get; set; } = 2.78f;
public string myString { get; set; } = "127";
private void button2_Click(object sender, EventArgs e)
{
//Get "myFloat" property of this instance of Form.
PropertyInfo myfloat_property = this.GetType().GetProperty("myFloat");
//Get ToString(IFormatProvider) method of the "myFloat" property.
MethodInfo to_string = myfloat_property.PropertyType.GetMethod("ToString", new Type[] { typeof(IFormatProvider) });
//Set "myString" property. Where i get the exception.
myString = (string)to_string.Invoke(myfloat_property, new object[] { CultureInfo.InvariantCulture });
}
}
I think i'm missing something easy to see. But i can't see it now, can you show me?
Thanks all.
Aucun commentaire:
Enregistrer un commentaire