?
Solved

Get Set Properties + Reflection

Posted on 2009-04-27
3
Medium Priority
?
671 Views
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

0
Comment
Question by:Sreedhar Vengala
  • 2
3 Comments
 
LVL 23

Expert Comment

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

Author Comment

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

Accepted Solution

by:
Jens Fiederer earned 1500 total points
ID: 24259651
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"
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

839 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