Edited Question: This should be clear.
using System;
namespace UpdateDateTimeFields
{
class Program
{
static void Main(string[] args)
{
string dateTimeFormat = "dd/MM/yyy HH:mm:ss";
DateTime time = DateTime.Now;
Update(time, dateTimeFormat); //Should update time
movemnet mymove = new movemnet();
mymove.FromDate = DateTime.Now;
mymove.ToDate = DateTime.Now;
mymove.Name = "Test_Movement";
Update(mymove, dateTimeFormat); //should update FromDate, ToDate
ParentClass cls = new ParentClass();
cls.mv.FromDate = DateTime.Now;
cls.mv.ToDate = DateTime.Now;
cls.CurrentDate = DateTime.Now;
cls.Comment = "ParentClass_Comment";
Update(cls, dateTimeFormat); //should update FromDate, ToDate, CurrentDate
}
private static void Update<T>(T Request, string format)
{
// How can this be acheived ???
//tried with Using Reflection to Get and Set values of Properties - NO JOY !!!
}
}
internal class movemnet
{
public DateTime FromDate { get; set; }
public DateTime ToDate { get; set; }
public string Name { get; set; }
}
internal class ParentClass
{
public movemnet mv { get; set; }
public DateTime CurrentDate { get; set; }
public string Comment { get; set; }
}
}
You should not do this. If you want your class to expose a property in a particular format, you should have your class do so. You should not expose any of your class’ fields. You should separate in your mind and in your code the data itself and the view of the data.
You should definitely not use reflection to iterate over each field in your class, check the type for DateTime and the set the format. You should definitely not do anything like …
I feel dirty just typing that code. 🙂