I have created a custom datatype using implicit operator below is the sample code. When I am setting hard coded value to my custom data type its working as expected, but setting value using reflection throwing conversion error. (System.ArgumentException: 'Object of type 'System.String' cannot be converted to type 'TF.DataType.TFDiplay'.')
Can someone help me what I need to change to work this with reflections well
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TF.DataType
{
public class TFDiplay
{
public TFDiplay() { }
public TFDiplay(object value, string text) { Value = value; Text = text; }
public TFDiplay(object value)
{
Value = value;
}
public static implicit operator TFDiplay(string value)
{
var result = new TFDiplay()
{
Value = value
};
return result;
}
public static implicit operator TFDiplay(int value)
{
var result = new TFDiplay()
{
Value = value
};
return result;
}
public object Value { get; set; }
public string Text { get; set; }
}
}
class Program
{
static void Main(string[] args)
{
TFDiplay tFDiplay;
tFDiplay = "ss"; //Working as expected
tFDiplay = 1; //Working as expected
testDatatype t = new testDatatype();
Type s = t.GetType();
PropertyInfo p = s.GetProperty("Flag");
p.SetValue(t, "ss"); //Throwing error
}
public class testDatatype
{
public TFDiplay Flag { get; set; }
}
}
Aucun commentaire:
Enregistrer un commentaire