Link to home
Start Free TrialLog in
Avatar of Sreedhar Vengala
Sreedhar VengalaFlag for Australia

asked on

Get Set Properties + Reflection

I need to format DateTime Fields on a object (nested Object)
Below is Code:
using System;
 
namespace UpdateDateTimeFields
{
    internal class Program
    {
        private static void Main()
        {
            const string dateTimeFormat = "dd/MM/yyy HH:mm:ss";
 
            var time = DateTime.Now;
            Update(time.AddMinutes(-1252), dateTimeFormat); //Should update time
 
 
            var mymove = new Movemnet {FromDate = DateTime.Now, ToDate = DateTime.Now, Name = "Test_Movement"};
            Update(mymove, dateTimeFormat); //should update FromDate, ToDate
 
 
 
            var newMove = new Movemnet { FromDate = DateTime.Now, ToDate = DateTime.Now, Name = "Test_Movement" };
            var cls = new ParentClass
                          {
                              Mv = newMove,
                              CurrentDate = DateTime.Now,
                              Comment = "ParentClass_Comment"
                          };
 
            Update(cls, dateTimeFormat); //should update FromDate, ToDate, CurrentDate
        }
 
        private static void Update<T>(T Request, string format)
        {
           // How to Achieve this 
        }
    }
 
    /// <summary>
    /// Movememt
    /// </summary>
    internal class Movemnet
    {
        public DateTime FromDate { get; set; }
        public DateTime ToDate { get; set; }
        public string Name { get; set; }
    }
 
    /// <summary>
    /// ParentClass
    /// </summary>
    internal class ParentClass
    {
        public Movemnet Mv { get; set; }
        public DateTime CurrentDate { get; set; }
        public string Comment { get; set; }
    }
}

Open in new window

Avatar of Jens Fiederer
Jens Fiederer
Flag of United States of America image

First of all, your first call is NOT going to achieve anything, no matter what.

You aren't giving it "time", you are giving it the value of a modified time that will be discarded upon return.

Second, HOW do you want the fields updated?   Do you want all datetime fields in nested classes set to the same value?

What value would that be, and why are you giving it a format?  Formats are used for making STRINGS (or parsing them), not DateTime fields.
Avatar of Sreedhar Vengala

ASKER

Hello Jensfiedere,
Task :

(In my Service Operation - which takes a request object (which is further nested  in with classes)
1. will be getting DateTime format from db (would be either dd/MM/yy HH:mm:ss (to strip milliseconds) or  dd/MM/yy HH:mm (to strip seconds))
2. Need to pass Request Object, DateTime format to a Method (Update<T>(T Request, string format) which futher gets the type of request and loop through all nested classes get fields of Type DateTime => format the datetime value as per the format passed.
eg: if Passed Request is CreateBoomStackStockpileMovement (containing Movement class, Integration Domain Class, BoomStack Class)
Movement got DateTime fields : StartDateTime, FinishDateTime
BoomStack got DateTime fields: OperationStartTime, OperationFinishTime
So in method  it modify request object by setting the Format of DateTime fields (both in Movement / BoomStack)  to the supplied format (either stripping milliseconds or seconds)

 Thinks this makes much more clear, what am doing.
Thanks for help
ASKER CERTIFIED SOLUTION
Avatar of Jens Fiederer
Jens Fiederer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial