Get Set Properties + Reflection

Posted on 2009-04-27
Last Modified: 2012-05-06
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

Question by:Sreedhar Vengala
    LVL 23

    Expert Comment

    by:Jens Fiederer
    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.
    LVL 9

    Author Comment

    by:Sreedhar Vengala
    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
    LVL 23

    Accepted Solution

    In other words, for each DateTime field f that occurs in the class or its child classes (does this include classes contained in containers that are part of the class?) you want to execute:

    f = DateTime.Parse(f.ToString(format))

    Of course, any later attempt to print those fields will NOT print them with the supplied format - it will just be printing zeroes in the "stripped-out" areas).

    THAT you can accomplish using reflection - there is no particular reason to use generics for it, it can be a function on "object".

    Of course, your first call would STILL have to be
                var time = DateTime.Now.AddMinutes(-1252);
                Update(ref time, dateTimeFormat); //Should update time - since DateTime is a value type, you need to pass it with a "ref"

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now