when using (int?) to convert, It succeed; when using reflection to convert, It failed; How can I assign value(enum?) to property(int?) success using reflection?
static void Main(string[] args)
{
Dc dc = new Dc { Solution = Solution.Upgrade };
Model model = new Model { };
//assign by reflection
var psolution = model.GetType().GetProperty("Solution");
//psolution.SetValue(model, dc.Solution); //this fail
model.Solution = (int?)dc.Solution; //this success
psolution.SetValue(model, Convert.ChangeType(dc.Solution, psolution.PropertyType)); //this fail
}
class Dc
{
public Solution? Solution { get; set; }
}
class Model
{
public int? Solution { get; set; }
}
enum Solution
{
Upgrade = 1,
Discard = 2,
}
Aucun commentaire:
Enregistrer un commentaire